2 ms·
Tail calls aren't always just used for some simple iteration. For example, you could have several mutually recursive functions calling each other in tail positi
by yorwba 9y ago
Tail calls aren't always just used for some simple iteration. For example, you could have several mutually recursive functions calling each other in tail position. If you wanted to turn that into a loop, you'd have to roll all those functions into a single loop body, which would be made even less elegant due to the lack of goto statement. (TCO essentially turns a call into a goto whenever possible.)
- viraptor 9y agoLots of languages can express it better though - even without gotos. For example in python you can do: while some_condition: x = one_generator(y) y = other_generator(x) where the generators yield values. No need for goto, no TCO, no magic. Even in languages like C, a nicer way to express it may be via two explicit state machines rather than going full Duff's device at this problem.
- lispm 9y agoPython's generators are more magic. It's similar to some kind of COME FROM mechanism.
- viraptor 9y agoWeird comparison. Come from has no indication on the other side that it will happen. Generators are pretty explicit with yield. On the calling side they can be explicit with a next() call.
- lispm 9y agoA generator may have multiple yields, if you call next(), then it comes from that call to the last yield call - based on the current execution context. The yield waits that the execution comes back to it. The idea of function calls is much simpler - no yield magic necessary.
- deleted 9y ago[deleted]