-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider implementing loops #5
Comments
Loops are based on mutable state. While mutable state technically exists in the interpreter, it's not available to Ketos code. I don't see how loops could work. However, the interpreter does implement tail recursion optimization, allowing infinite recursion without filling up the stack. This definitely should have been mentioned in the docs and I'll write something up about it straight away. |
👍 for TCO instead of loops. After that it would be nice to have macros like |
Under which conditions is TCO guaranteed? The factorial example blows up the stack rather quickly... |
The code in In low-level terms, tail call optimization is guaranteed any place where a recursive call occurs immediately before a In high-level terms, any recursive function call in a tail position should become a tail call. If it doesn't, it's a bug. Note that this only applies to recursive calls. |
Easy!
|
@qthree: It looks like a loop (kind of), but there's no mutable state being modified in the body. That's what I was saying wouldn't work. Local bindings can't be modified. Maybe you could do something crazy with |
But that is how loops In Lisps works, almost. In Scheme there are loop macros which translates almost to that (they use Łukasz Jan Niemier Dnia 16 mar 2016 o godz. 05:27 Murarth [email protected] napisał(a):
|
I really don't think loops are necessary. However, I think a lot of imperative programmers find comfort in having some kind of list comprehension syntax, sort of like Elixir's for. |
List comprehension and perhaps other styles of loop could be implemented via |
@murarth alternatively you could provide only general |
@hauleth: It's conceivable, yes, but I don't believe it would be of much use without the ability to mutate values and/or bindings. In Common Lisp implementations, values can be mutated. Ketos, however, uses a model of shared ownership and copy-on-write. (Some Without general mutation, imperative programming styles are less effective and a |
For what it's worth, I think a construct that implicitly repeats a block of code (unless you tell it not to) is less intuitive than a construct that executes a block of code once (unless you tell it to execute again). Really, loop-with-break is functionally identical to run-with-rerun, just with the logic inverted. Fixed-point combinators let you write loops without explicitly using recursion, which should keep everyone happy (?):
(This example blows up the call stack at around 350 or so for me, but you get the idea.) |
As far as I know, it is currently not possible to implement loops without resorting to recursion, which is limited by the stack size. It would be useful to have efficient loops in the language.
The text was updated successfully, but these errors were encountered: