Iterative vs. Recursive coding

John Bokma john at castleamber.com
Sat Aug 21 20:09:52 EDT 2010


John Nagle <nagle at animats.com> writes:

> On 8/20/2010 1:17 PM, John Bokma wrote:
>> John Nagle<nagle at animats.com>  writes:
>>
>>>      Python does not do tail recursion, so using recursion
>>> where iteration could do the job is generally a bad idea.  Scheme, on
>>> the other hand, always does tail recursion where possible.
>>
>> I think you mean tail recursion optimization / elimination.
>> Python does tail recursion:
>
>    Not very well.

Based on your reply that follows, I agree.

>     def cnt(n) :
>         if n > 0 :
>             cnt(n-1)
>
>
> This will work for up to cnt(998), but at cnt(999), CPython
> reports "RuntimeError: maximum recursion depth exceeded."
>
> Yes, you can increase the recursion depth, but that case
> shouldn't be compiled to recursion at all.

I agree: so this means that Python should eliminate / optimize tail
recursion. 

To me, the current value seems a bit low, but I know nothing about
Python internals.

-- 
John Bokma                                                               j3b

Blog: http://johnbokma.com/    Facebook: http://www.facebook.com/j.j.j.bokma
    Freelance Perl & Python Development: http://castleamber.com/



More information about the Python-list mailing list