Self function

Scott David Daniels Scott.Daniels at Acm.Org
Mon May 4 18:31:01 EDT 2009


Arnaud Delobelle wrote:
> In that case the following would not grow the stack, given tail-call
> optimization:
> 
> def visit(node):
>     print 'visiting', node
>     if node.right is None:
>         return visit(node.left)
>     if node.left is not None:
>         visit(node.left)
>     return visit(node.right)
> 
Or (without TCO):
     def visit(node):
         while node is not None:
             print 'visiting', node
             if node.right is None:
                 node = node.left
             else:
                 if node.left is not None:
                     visit(node.left)
                 node = node.right

Not so hard, really.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list