4 ms·
Whether it does tail call optimization is a different question than whether you can write the algorithm in a tail-recursive manner, though.
by gloob 17y ago
Whether it does tail call optimization is a different question than whether you can write the algorithm in a tail-recursive manner, though.
- steveklabnik 17y agoYes, but without the optimization, how would it help? Am I just being a n00b?
- apgwoz 17y agoNo, you're not being a n00b. It'll help because the stack usage will grow linearly as opposed to quadratically, as in the case of the naive solution. You'll still overflow the stack without the optimization.
- mbrubeck 17y agoWriting it tail-recursively forces you to switch from the O(2^n) algorithm (which has two recursive calls; you can't put both into tail position) to a more efficient algorithm. You could also write the more-efficient algorithm without tail recursion, of course.