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

2

u/Sumandora Jan 18 '26

Isn't this what you are looking for https://llvm.org/doxygen/TailRecursionElimination_8cpp_source.html ?
I believe it's part of LLVM, it would also make little sense if it wasn't since the frontend is precisely not supposed to think about this kind of codegen work. You can also force tailcalls from the frontend, at least Clang can (and I would assume that clang doesn't get some magic API for this, that other frontends don't have): https://clang.llvm.org/docs/AttributeReference.html#musttail

Perhaps this was true a few years back, but now LLVM is certainly capable of doing tail call optimization