Python is not bad ;-)

Christian Gollwitzer auriocus at gmx.de
Sat May 2 07:32:59 EDT 2015


Am 02.05.15 um 13:21 schrieb Chris Angelico:
> On Sat, May 2, 2015 at 9:07 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
>> I need to add, I grew up with imperative programming, and as such got
>> recursion as the solution to problems that are too complex for iteration,
>> i.e. tree traversal and such, and exactly these are never tail-recursive.
>
> Tree traversal can be forking-recursive:
>
> def walk(func):
>      if self.left and self.left.walk(func): return 1
>      if func(self.payload): return 1
>      if self.right: return self.right.walk(func)
>
> The left walk is non-tail-recursive, but the right walk is. This
> particular style looks rather less clean with iteration:

Accepted. In case your tree is heavily imbalanced, i.e. it is a list 
pretending to be a tree, the tail-recursion can cut down the memory 
usage significantly. It does not help, though, in the general case of a 
balanced tree or if the tree leans to the left side. That's why I still 
think it is a microoptimization, which helps only in some specific cases.

	Christian



More information about the Python-list mailing list