Python's "only one way to do it" philosophy isn't good?

Anders J. Munch 2007 at jmunch.dk
Tue Jun 12 23:11:45 CEST 2007


Paul Rubin wrote:
> Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> writes:
>>> Not tail calls, in general, no.
>> Sorry, how does that work? You're suggesting that there is an algorithm
>> which the compiler could follow to optimize away tail-recursion, but human
>> beings can't follow the same algorithm?
>>
>> Now I'm confused.
> 
> The usual compiler method is to translate the code into
> continuation-passing style and thereby gain tail-recursion
> optimization automagically.  

There's no need to go into CPS just to optimise tail-recursion.  After all, 
compilers were optimising tail-calls decades before Appel's work on SML/NJ.

Converting tail-recursion to iteration is trivial, and perfectly reasonable for 
a human to do by hand.  You add an outer "while True"-loop, the recursive call 
becomes a tuple assignment, and other code paths end with a break out of the 
loop.  Completely mechanical and the resulting code doesn't even look that bad.

Like Steven said, tail-call optimisation is not necessary as you can always 
hand-optimise it yourself.

- Anders




More information about the Python-list mailing list