r/programming • u/fagnerbrack • Apr 09 '21
How Tail Call Optimization Really Works - Forget About Recursive Functions
https://eklitzke.org/how-tail-call-optimization-works5
u/pfp-disciple Apr 09 '21
I first saw TCO (essentially) in the Apple // 6502 assembly code. A subroutine would JMP to another subroutine instead of JSR. I was young, and thought it was a quite clever approach to saving the stack and time.
4
4
u/Jautenim Apr 09 '21
An excerpt of Structure and Interpretation of Computer Programs (1985) that stuck with me, because I first heard about TCO roughly at the same time:
"In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure. When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers (either directly or indirectly) to the procedure itself. But when we describe a process as following a pattern that is, say, linearly recursive, we are speaking about how the process evolves, not about the syntax of how a procedure is written. It may seem disturbing that we refer to a recursive procedure such as fact-iter as generating an iterative process. However, the process really is iterative: Its state is captured completely by its three state variables, and an interpreter need keep track of only three variables in order to execute the process.
One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose "looping constructs" such as do, repeat, until, for, and while. The implementation of Scheme we shall consider in chapter 5 does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tail-recursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar."
In other words, tail call optimized functions are simply obfuscated loops.
3
u/Dragdu Apr 09 '21
In other words, tail call optimized functions are simply obfuscated loops.
Only tail recursive functions. Functions can tail call other, different functions, without ever recursing.
2
u/Jautenim Apr 09 '21
Yes. What I meant to say is that for a short time I went crazy and tried to twist and bend my functions so that they were tail recursive "because it is so much more efficient". Then I realized I was just writing obfuscated loops.
Now I only try to use recursive procedures for truly recursive processes. Which cannot be tail-call optimized anyway.
2
u/glacialthinker Apr 09 '21 edited Apr 10 '21
Your quote from SICP is excellent. Though I disagree with your conclusion.
Following the SICP comment I can flip the subject title: TCO lets me forget about iteration (and its necessity for mutable state).
I prefer recursive functions rather than a loop because it's clear what state is "part of the loop" as it's fed back into the recurrence: it's an argument of the recursive function. This is assuming immutable values are the default (ie. the special case of mutation is syntactically notable).
1
1
u/JB-from-ATL Apr 09 '21
So TCO is sort of like in lining. Like half of it, since you still jump to the other place.
1
13
u/pitkali Apr 09 '21
While I have usually encountered the concept in the context of recursion, I was never under the impression that there is anything here that is made harder to explain or understand because of the recursion, as the author seems to imply.