8 Queens Problem

Jonathan Hogg jonathan at onegoodidea.com
Tue Jul 9 13:08:28 EDT 2002


On 9/7/2002 17:03, in article
mailman.1026230956.30903.python-list at python.org, "Justin Sheehy"
<justin at iago.org> wrote:

>>     Would this solution qualify as tail-recursive?
> 
> Yes.  The only recursive call that you make is in tail position.

I'd say no - because the algorithm relies on continuing execution after
return of the recursive call (the outer for loop continues).

With tail-recursive code you can safely throw the current stack frame and
replace it with a new frame for the recursive call. When that call returns
it will return to the original caller (make sense?). This means that a tail
recursive function can be optimised to not consume stack - and thus become
iterative.

A tail-recursive function is one that has the pattern:

    def f( ... ):
        ...
        return f( ... )
        ...

i.e., there should be no recursive call that doesn't immediately return the
result of that call.

But yes, Python doesn't optimise the tail-recursion away so it doesn't
matter.

Jonathan




More information about the Python-list mailing list