Iterative vs. Recursive coding
John Bokma
john at castleamber.com
Sun Aug 22 14:18:36 EDT 2010
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> On Sat, 21 Aug 2010 19:09:52 -0500, John Bokma wrote:
>
>> this means that Python should eliminate / optimize tail
>> recursion.
>
> There have been various suggestions to add tail recursion optimization to
> the language. Two problems:
[snip]
> But this is not the only sort of tail-call recursion, and a traceback
> like the following is useful:
>
>
>>>> recurse(4)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 5, in recurse
> File "<stdin>", line 3, in f
> File "<stdin>", line 5, in recurse
> File "<stdin>", line 3, in f
> File "<stdin>", line 5, in recurse
> File "<stdin>", line 3, in f
> File "<stdin>", line 4, in recurse
> File "<stdin>", line 2, in g
> ValueError
>
>
> If all you saw was the last line (the call to g), debugging the exception
> would be significantly harder.
Yup, agreed, good example.
> Me personally, I'd like to see either a (preferably) run-time setting or
> compile-time switch that enables/disables this optimization. Even an
> explicit decorator would be fine. And lo and behold:
>
> http://hircus.wordpress.com/2008/06/21/python-tail-call-optimization-done-right/
> http://groups.google.com/group/comp.lang.python/msg/9b047d1392f2b8ec
>
>
> Add it to your bag of tricks and have fun.
Thanks for the links. And yes, I will add this to my bag of tricks
(aka local wiki with notes ;-) ).
--
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