Iterative vs. Recursive coding

John Bokma john at castleamber.com
Fri Aug 20 16:17:22 EDT 2010


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:

>>> def x(n):
...     if n == 10: return
...     print n
...     x(n + 1)
... 
>>> x(1)
1
2
3
4
5
6
7
8
9


-- 
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