r/ProgrammingLanguages • • Jan 18 '26

Why not tail recursion?

In the perennial discussions of recursion in various subreddits, people often point out that it can be dangerous if your language doesn't support tail recursion and you blow up your stack. As an FP guy, I'm used to tail recursion being the norm. So for languages that don't support it, what are the reasons? Does it introduce problems? Difficult to implement? Philosophical reasons? Interact badly with other feathers?

Why is it not more widely used in other than FP languages?

76 Upvotes

117 comments sorted by

View all comments

Show parent comment

1

u/initial-algebra Jan 19 '26

With explicit iteration (or a self-tail-call), the stack has enough information to immediately see all the code that the execution path went through to arrive at the current state.

Kinda, but also no. If the loop body contains a branch that depends on the loop state, that's basically the same as sibling recursion.

Occasionally I feel the need to turn it off — SBCL has a way to do this on individual callers — to debug something.

Oh, I agree that automatic TCO is probably the wrong thing to do in a debug build, except when the programmer explicitly marks a tail call as "must optimize". "Incidental" tail calls, where there's no danger of blowing the stack (or causing a space leak, with GC'd stack frames) are probably pretty rare in practice, though.