Tail recursion to while iteration in 2 easy steps

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Oct 2 09:32:29 EDT 2013


On Wed, 02 Oct 2013 08:31:25 -0400, random832 wrote:

> On Tue, Oct 1, 2013, at 17:30, Terry Reedy wrote:
>> Part of the reason that Python does not do tail call optimization is
>> that turning tail recursion into while iteration is almost trivial,
>> once you know the secret of the two easy steps. Here it is.
> 
> That should be a reason it _does_ do it - saying people should rewrite
> their functions with loops means declaring that Python is not really a
> multi-paradigm programming language but rather rejects functional
> programming styles in favor of imperative ones.

Python is not as aggressively functional as (say) Haskell, but it is 
surely an exaggeration to suggest that the failure to include tail call 
optimization means that Python "rejects" functional programming styles. 
You can still write you code in a functional style, it just won't be as 
heavily optimized.


-- 
Steven



More information about the Python-list mailing list