Teaching : Python, Scheme, Java...

Alex Martelli aleax at aleax.it
Fri Apr 18 09:53:30 EDT 2003


Donnal Walter wrote:

> Steven Taschuk wrote:
>> Quoth Jean-Paul Roy:
>>>- Python is bad : tail recursion is not iterative (also astonished at
>>>that, I can understand with an interpreter, but compiler ?)
>> 
>> 
>> For teaching purposes, imho tail recursion is a Bad Thing.
>> 
>> Writing an iterative process using tail recursion has always
>> struck me as a clever but obfuscated technique.  Write what you
>> mean: if you want an iterative process, write a loop; if you want
>> a recursive process, make recursive calls.
> 
> What is the difference between "recursive calls" and "tail recursion"?
> Sometimes I have written methods that call the same method on a parent
> or child object, with the depth of recursion being limited by a method
> of the same name NOT calling itself. In the past I have thought of this
> as tail recursion. Is this incorrect?

A function-call is "tail" if it's the last thing the caller does,
i.e., basically, if the caller executes "return somefunction(args)"
or a semantically equivalent sequence.

A function-call is "recursive" if it's a call of the function to
itself (there's also "mutual recursion", where two functions call
each other).

A function-call is "tail recursive" if it's tail and recursive.
Such calls can be optimized by avoiding the allocation of a new
stack frame -- the same frame object that's already on the
stack to handle this specific call can be re-used (if you're
willing to lose traceback information -- Python currently does
not allow that).

Your example is not recursive: the fact that the various functions
being called have the same name is accidental, as they're different
functions anyway.


Alex





More information about the Python-list mailing list