Reputation: 735
Is it theoretically possible to transform every kind of general-recursion into tail-recursion? Are they equivalent for example from a lambda-calculus point of view? That's a debate between me and an acquaintance.
My view is that it's not possible everytime. For example if you have a function which calls itself recursively twice or three times, then you can't make all the recursive calls into tail-calls, right? Or is there always a way to reduce the number of recursive calls to one single recursive call?
Upvotes: 2
Views: 93
Reputation: 48745
No. If you cannot rewrite your algorithm to have only tail calls, like in tree traversal, at least one call will not be in tail position.
Some may say loop + explicit stack is iterative, but IMO it's still recursion and a tree traversal will grow the stack just as general recursion.
Upvotes: 0