[Python-ideas] Tail Call Optimization (was Re: Tail recursion elimination)

Steven D'Aprano steve at pearwood.info
Mon Jan 20 01:53:37 CET 2014


On Sun, Jan 19, 2014 at 10:31:06PM +1000, Nick Coghlan wrote:

> Guido is on record as preferring iterative algorithms as more
> comprehensible for more people, and explicitly opposed to adding tail
> call optimisation. 

Many people struggle with recursion. Many people struggle with 
couroutines, and asychronous programming, and Unicode. Some people never 
quite get the hang of object oriented programming. That doesn't imply 
that Python should only offer features which nobody struggles with. It 
would be a pretty bare language if that were the case :-)


> I tend to agree with him - functional programming
> works OK in the small (and pure functions are a fine tool for managing
> complexity), but to scale up in a way that fits people's brains, you
> need to start writing code that looks more like a cookbook.

Python is not a pure functional language. Adding TCE won't make it one. 
If somebody wants to write their app in a pure functional manner, 
they're either not going to use Python at all, or they'll do it 
regardless of the lack of TCE and just grumble that Python is only 
suitable for "toy" applications.

But as a *component* of a larger "cookbook" style application, pure 
functions are great. And some functions are more naturally written in 
recursive style rather than iterative. I have no interest in writing my 
entire app as a pure-functional app (if I wanted to do that, I'd use 
Haskell). But I do have great interest in being able to write functions 
in the most natural way possible, and that sometimes means recursively, 
without having to compromise for performance.


> If you want inspiration on how to design a language for typical human
> thought patterns, look to cookbooks, training guides and operator
> manuals, not mathematics.

And Python is a great example of that, but it's not really relevant to 
the idea of adding TCE. Or at least, its no more relevant than are 
people's grumbles that adding such things as closures and coroutines 
makes Python more complex and too advanced for "ordinary programmers".

Adding TCE need not affect Python as a language. People who like 
iteration will still write iterative functions. People who think like 
Java programmers will still write Java in Python, people who think like 
bash scriptors will still write bash in Python. The only addition is 
that people who think like Scheme programmers will have one less thing 
to complain about Python *wink*

Most programmers write for themselves, or for a small group. Arguing 
that Sue (who can think recursively) ought to write her code using an 
iterative algorithm because Tom and Jerry won't otherwise understand it 
is not a terribly strong argument when Tom and Jerry aren't in Sue's 
target audience.


-- 
Steven


More information about the Python-ideas mailing list