4 ms·
Yep Tail Call "Optimization" is nice. But what about recursive functions that cannot be tail call "optimized"? First example that comes to mind is the Tower of
by linkdd 6y ago
Yep Tail Call "Optimization" is nice. But what about recursive functions that cannot be tail call "optimized"?
First example that comes to mind is the Tower of Hanoi algorithm:
def toh(ndisk, source, via, target):
if ndisk > 0:
toh(ndisk - 1, source, target, via)
target.append(source.pop())
toh(ndisk - 1, via, source, target)
The function calls itself 2 times. Same thing is the fibonacci function which relies on the 2 previous results.
Here, you'll certainly need memoization and other techniques to not blow up the stack.