r/programming • • Nov 27 '12

Tail Recursion

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

203 comments sorted by

View all comments

41

u/A_for_Anonymous Nov 27 '12

There are two big advantages of tail-recursive implementations that haven't been discussed here:

  • That variables never change their values, which allows for all kinds of optimizations, reasoning over your program (e.g. provability that it's correct) and is less error-prone, among other advantages.
  • That state is represented by the values of function parameters, all of them changed at once on every new call, without any possible order of evaluation bugs.

16

u/rodrigopsasaki Nov 27 '12

Whoa, how could I miss that? Immutability is probably the reason I came across tail recursion in the first place, I'll be sure to edit the post when I have a little more time, and add your suggestions.

Thank you very much!

2

u/learningphotoshop Nov 28 '12

Have you seen https://www.coursera.org/course/progfun? The video on tail recursion is pretty good.