Iterative vs. Recursive coding
John Nagle
nagle at animats.com
Sat Aug 21 02:37:12 EDT 2010
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.
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.
John Nagle
More information about the Python-list
mailing list