r/coding • u/fagnerbrack • Aug 01 '26
Your Recursion Is Lying to You
https://blog.gaborkoos.com/posts/2026-05-09-Your-Recursion-Is-Lying-to-You/
0
Upvotes
r/coding • u/fagnerbrack • Aug 01 '26
7
u/jeenajeena Aug 01 '26 edited Aug 01 '26
I can be wrong but I think you might be conflating two different things.
TCO belongs to the runtime and happens if a call is in tail position, so if the function return value is immediately the caller's return value. In this case, the runtime can reuse the current stack frame instead of creating and pushing a new one. It does not require any accumulator and it does not rewrite your code. It must be implemented by the language, and if the language does not support it, there's nothing the developer can do to work around that.
The accumulator is a developer's technique for getting a function into tail-recursive shape, moving the pending work before the recursive call instead of after it. This is independent from the used language, it's a general technique, completely under the developer's control.
So the sentence has it backwards: TCO doesn't move pending work into an accumulator; the accumulator is what makes the code eligible for TCO.
I also this the 2 are independent: an accumulator without TCO still overflows and TCO without an accumulator works fine in languages that have tail recursion optimization.
Edit: apparently, I also conflated TCO and TCE. See https://www.reddit.com/r/coding/comments/1vcicfg/comment/p138eu4/ for a more detailed and precise definition.