4 ms·
Decent explanation, but I have a couple quibbles. First, the term "tail recursion" is overly narrow. This transformation should apply to all tail calls, not ju
by sdevlin 14y ago
Decent explanation, but I have a couple quibbles.
First, the term "tail recursion" is overly narrow. This transformation should apply to all tail calls, not just recursive ones. This is useful, for example, in families of mutually recursive functions.
Second, this transformation is not really an optimization, as it changes the behavior of some programs (by preventing stack overflow or memory allocation errors). It's a documented feature of languages (or specific implementations of languages) that users rely on to write programs in a particular style.
I prefer the term "tail call elimination" to describe these transformations.
- kvb 14y agoOf course, some languages only support elimination of tail calls when they're recursive. I believe that Scala suffers from this (since the JVM doesn't support tail call elimination).
- sdevlin 14y agoGood point, I'm not very familiar with Scala. I think Clojure may also have this limitation.