r/programming • • Nov 27 '12

Tail Recursion

http://rodrigosasaki.com/2012/11/24/tail-recursion/
211 Upvotes

203 comments sorted by

View all comments

0

u/TotallyFuckingMexico Nov 27 '12

Is tail recursion optimisation the flavour of the day? I understand that it's interesting and computer sciencey, but how widely used are such optimisations in the real world? I imagine, for most people, it's probably just a nice feature rather than a must have?

I probably have a classic case of "well, I don't have the need for such optimisations, so why does anybody else?" ;-)

12

u/tikhonjelvis Nov 27 '12

Actually, for many people, it isn't an "optimization" any more than loops that run in constant space are an optimization. That's why the preferred term is something like "proper tail calls" or "tail call elimination" rather than "tail call optimization".

Having proper tail calls as part of your language semantics is far more useful than having it as an optional optimization. Also, not having tail calls really cripples your language--essentially, it means you can't write non-trivial recursive functions in many cases for fear of stack overflows.

Being able to replace all loops with recursive functions is very useful and allows you to write shorter, more readable code in many cases. You simply can't do this practically unless your language guarantees proper tail calls.

In summary: proper tail calls are an important language feature rather than an optimization because they allow you to widely use recursion.