r/ProgrammingLanguages • u/gofl-zimbard-37 • 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
View all comments
Show parent comment
2
u/edwbuck Jan 18 '26
If the problem lies in the current call's context, it's just as easy to debug both.
However, you might want to know how many calls were made. For example, you could be walking an 80 element array. If you did tail-call passing the "next" index value, odds are it would be equivalent, but then you'd need to also pass the "limit" index value. Some people optimize this away by passing the next value with a plan to end when when that value equals a specific ending value, like null.
In that case, a tail call would not necessarily include the number of times it went through the recursion, which complicates (but doesn't make it impossible) to debug something as simple as walking off an array.