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?
75
Upvotes
View all comments
Show parent comment
1
u/edwbuck Jan 19 '26
The Mariner 1 only needs to blow up once.
No matter what your personal experience is, a language should reduce the ways we write things to ways that a computer can run them in non-unexpected ways, and preferably in non-error permitting ways.
You might just be a very disciplined software developer. If so, that's great! However, with enough time and use, people discover were the less disciplined developers stumble. In writing recursive visitors to a list, I personally will never read all the potential stack frames that could be lost, but I'd like a few at the beginning, and a few at the end, and if there was some sort of choice down a different path, a few around that choice.
Reusing the frame makes it a runtime requirement to destroy this information. Will I need it in 98% of the code I've written this year? No. Have I had a scenario over my career where it would have been useful? Yes, otherwise I wouldn't be talking about it now.
Languages both propel developers to new heights of efficiency and thankfully rarely thwart them in understanding what oversights they committed in writing their code. Aggressive optimization can make debugging a chore, because the code running doesn't resemble the code written. Stack frame reuse is just one of many examples.
Honestly, I can't figure out why people find this so controversial. It's like they haven't built up enough experience for it to impact them, so they pretend it doesn't exist.