functional programming

Terry Reedy tjreedy at udel.edu
Sun Feb 27 00:34:31 EST 2000


"bjorn" <bjorn at roguewave.com> wrote in message
news:38B2CE44.CB9B41A9 at roguewave.com...
>Maybe I'm dense, but what is stopping us from implementing tail call
>optimization (of which tail recursion is just a special case) with the
current
>Python implementation.  Identifying a call in a tail position is a simple
>static analysis that isn't affected by Python's dynamicity (is that a
word?).

But as Guido has pointed out, the recursiveness of the call is.  Consider
the following apparently foolish but currently legal code:

>>> def g(n): print n
...
>>> def f(n):
...   f = g
...   return f(n-1)
...
>>> f(3)
2

>>> def f(n):
...   global f
...   f = g
...   return f(n-1)
...
>>> f(3)
2
>>> f
<function g at 82ddb388>
>>> f(3)
3
>>>

In either case, f looks recursive but is not, so any optimizer would have
to identify such exceptions.

Terry J. Reedy






More information about the Python-list mailing list