Explain to me what the big deal with tail call optimization is and why Python needs it

So apparently, there’s been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn’t “get it.” I’m in the same boat as Guido. I understand the concept of tail call optimization. I just can’t think of any reason why Python really needs it.

To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO?

What Is Tail Call Optimization?

Very simply, what is tail-call optimization? More specifically, Can anyone show some small code snippets where it could be applied, and where not, with an explanation of why?

Why is Garbage Collection Required for Tail Call Optimization?

Why is garbage collection required for tail call optimization? Is it because if you allocate memory in a function which you then want to do a tail call on, there’d be no way to do the tail call and re

What is tail recursion optimization? [duplicate]

Possible Duplicate: What is tail-recursion? What is tail recursion optimization?

tail call optimization in lua

Lua claims that it implement tail call properly thus no stack needs to be maintained for each call thus allow infinite recursion, I tried to write a sum function, one is not tail call, and one is tail

Tail call optimization in Mathematica?

While formulating an answer to another SO question, I came across some strange behaviour regarding tail recursion in Mathematica. The Mathematica documentation hints that tail call optimization might

Why won’t the Scala compiler apply tail call optimization unless a method is final?

Why won’t the Scala compiler apply tail call optimization unless a method is final? For example, this: class C { @tailrec def fact(n: Int, result: Int): Int = if(n == 0) result else fact(n – 1, n * re

Measuring performance after Tail Call Optimization(TCO)

I have an idea about what it is. My question is :- 1.) If i program my code which is amenable to Tail Call optimization(Last statement in a function[recursive function] being a function call only, no

What is the current state of tail-call-optimization for F# on Mono (2.11)?

What is the current state of Tail Call Optimization (TCO) implementation on Mono (2.11) ? Read somewhere that all the codebase would need to be modified to use a callee-pops-arguments convention. What

C tail call optimization

I often hear people say that C doesn’t perform tail call elimination. Even though it’s not guaranteed by the standard, isn’t it performed in practice by any decent implementation anyhow? Assuming you’

Tail Call Optimization in Mono/Ironpython

given that CLR supports tail call optimization I was wondering if Mono, and consequently Ironpython running on Mono has support for TCO ?


Tail call optimization makes it easier to write recursive functions without worrying about a stack overflow:

def fac(n, result=1):
        if n > 1:
                return fac(n - 1, n * result)
        return result

Without tail call optimization, calling this with a big number could overflow the stack.

Personally, i put great value on tail call optimization; but mainly because it makes recursion as efficient as iteration (or makes iteration a subset of recursion). On minimalistic languages you get huge expressive power without sacrificing performance.

On a ‘practical’ language (like Python), OTOH, you usually have a lot of other constructions for almost every situation imaginable, so it’s less critical. Always a good thing to have, to allow for unforeseen situations, of course

If you intensely want to use recursion for things that might alternatively be expressed as loops, then “tail call optimization” is really a must. However, Guido, Python’s Benevolent Dictator For Life (BDFL), strongly believes in loops being expressed as loops — so he’s not going to special-case tail calls (sacrificing stack-trace dumps and debugging regularity).

Guido recognized in a follow up post that TCO allowed a cleaner the implementation of state machine as a collection of functions recursively calling each other. However in the same post he proposes an alternative equally cleaner solution without TCO.

I have been thinking to your question for years (believe it or not…). I was so deeply invested in this question that I finally wrote a whole article (which is also a presentation of one of my modules I wrote some months ago without taking the time to write a precise explanation of how to do it). If you are still interested in that question, please read my answer on my blog. In two words, I give a presentation of the tco module; you will not find anything that you can’t already do without tail-recursion elimination, but you may be interested by my thoughts about it.

I know that mere links are not the preferred usage on Stackoverflow; please consider however that I wrote a whole article for answering to this post (which I refer to in the body of my article) including also some pictures for illustrating it. For this reason, I post here this unusual answer.