16 ms·
The first example is really dangerous. We recently had a issue with GCC not optimizing a tail recursion in a relatively large function with parameters passed in
by nikita 12y ago
The first example is really dangerous. We recently had a issue with GCC not optimizing a tail recursion in a relatively large function with parameters passed in as references to std collections. The result - it was running out of stack space at large scale. So if you make it to a habit of using tail recursion extensively GCC can bite you where you don't expect.
Of course adding a unit test for every function saved the day as usual, however it is trivial to convert tail recursion into a loop and not worry about stack overflow.
- Dewie 12y agoIt's nice to be able to annotate functions that should have some optimization done to it, and then the program fails to compile if it isn't able to perform the optimization.
- mpartel 12y agoScala has this: http://www.scala-lang.org/api/current/index.html#scala.annotation.tailrec http://www.scala-lang.org/api/current/index.html#scala.annot...
- Dewie 12y agoI've used it when implementing a game "loop" (just continually asking user for input to the game). It's nice to be able use recursion when you want to, and be sure that the call stack won't grow larger just by playing the game for several rounds.
- SamReidHughes 12y agoIt never makes sense to attempt tail recursion in C++, where it's so easy for destructors to get in the way.