3 ms·
Tail call elimination doesn't let you do guarded (co-)recursion. For example, in a strict language you can't have a corecursively defined list like loop a b
by wyager 6y ago
Tail call elimination doesn't let you do guarded (co-)recursion. For example, in a strict language you can't have a corecursively defined list like
loop a b = a : loop b (a + b)
fibs = loop 0 1
Because tail call elimination can't do anything about the first case.
- yawaramin 6y agoThis example actually does not have a tail-recursive call, so if you mean that a non-tail recursive call is not stack safe in a strict language, that is correct, yes.
- dwohnitmok 6y agoI think wyager's point is that you can extract individual Fibonacci numbers from fibs rather than realizing the infinite list immediately.
- wyager 6y agoRight - my point is that tail call elimination doesn’t help you with guarded corecursion, which lazy languages handle just fine. Guarded corecursion represents a huge set of programs it’s useful to be able to evaluate.
- slaymaker1907 6y agoBoth function calls are in tail position. This is what actually matters (along with the ABI return type matching for runtimes where that matters).
- dwohnitmok 6y ago'loop' is not in a tail position. The last operation is the list cons. It can be optimized with TCO modulo cons but that would just make it hang/exhaust heap rather than stack overflow in a strict language.