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

Neil Cerutti horpner at yahoo.com
Wed Jun 13 08:15:18 EDT 2007


On 2007-06-12, Anders J. Munch <2007 at jmunch.dk> wrote:
> 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.  

For simple recursive tail calls, yeah, it can be. Translating a
tail-recursive Factorial function into a while loop is easy. But
tail-call optimization technically works for any tail-call,
including mutual recursion, and non-recursive tail-calls. You
can't reasonably hand-optimize away the stack frame for all
tail-calls.

def foo(x)
  bar(x)

The only way to hand-optimize the call to bar is to inline it
yourself.

-- 
Neil Cerutti
Will the highways on the Internet become more few? --George W. Bush



More information about the Python-list mailing list