Comment by pdpi
12 hours ago
> For example, you need to manage a stack to do iteration on your binary tree
Recursion around trees can get you into trouble with the stack. Consider:
def traverse(node):
do_work(node.payload)
traverse(node.left)
traverse(node.right)
The second recursive call to traverse is in tail position and is a candidate for elimination, but the first one isn't and will _always_ eat stack space. This is fine if you know what you're doing, but can bite you in the arse if you're expecting TCO to save you.
TCO stands for "TAIL call optimization". It will obviously not save your arse if the call is not in the tail of the function, exactly the same as "python generators will not help you if you are doing C++".