r/programming • • Apr 09 '21

How Tail Call Optimization Really Works - Forget About Recursive Functions

https://eklitzke.org/how-tail-call-optimization-works
26 Upvotes

17 comments sorted by

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.

13

u/AttackOfTheThumbs Apr 09 '21

There's this belief that recursion is a hard concept and stumps people.

But I really don't see it that way.

4

u/Prod_Is_For_Testing Apr 09 '21

I think normal loops are usually easy to read than the equivalent recursion. But most of the issue is that undergrad students are told that recursion is hard before they actually do it so then they lose focus

1

u/Dr_Findro Apr 10 '21

I think recursion is a really strong example of the light switch. I could not grasp recursive problem solving for a little while and then after some practice, it clicked and I could recursively solve relevant problems effortlessly. It wasn’t like other programming concepts where I had a slower, but linear improvement path.

1

u/fagnerbrack Apr 14 '21

I understand the concept of TCO, but the explanation in lower level has enlightened me with another perspective definitely

1

u/[deleted] Apr 10 '21 edited Apr 10 '21

Lucky you. One suffers from the same affliction as the author, despite fully understanding recursion. The problem, for me, boils down to the fact that recursion does work implicitly and can feel like magic because of the fact that it is essentially hiding work. This is a problem for me when trying to understand a new algorithm because certain steps that are laid out in linearly in the RFC or whatever can be implicitly accomplished via recursion. When such steps are so hidden, I instinctually hunt for them until I finally figure out that I'm a moron.

That said, I'm perfectly happy to use recursion after I've learned to solve a particular problem without it.

TLDR; I really appreciate what the author is trying to do for others like myself here.

1

u/pitkali Apr 10 '21

Frankly, the point of my comment was not that recursion was easy. It may be to me, but I won’t claim that should make it as easy for others.

The point was that there is nothing special about TCO in the context of recursion. It changes a function call to a jump. Whether the jump happens to the beginning of the same or other function seems to be a trivial difference to me that should not affect explanation or understanding of what it does.

1

u/[deleted] Apr 10 '21

Where did one say that recursion was hard? The problem isn't difficulty, it's that recursion and TCO are completely separate and unrelated subjects that just happen to complement each other. Yet, the overwhelming majority of people who try to teach the subject seem to deeply tie the two together. In fact, it actually took me a couple of years to finally understand that TCO could be used in other contexts because I'm dumb and never thought to separate the two concepts.

You're absolutely right that there isn't anything special about TCO in the context of recursion but it isn't TCO that one struggles with. It's that my brain melts whenever an attempt is made to mentally execute all but the most trivial of recursive functions. When trying to learn something new, or refine existing knowledge, one has a much easier time when examples can be given that don't involve mentally inserting push and pop calls. Teach me using a simple loop and then let me notice that the stack manipulation can be sweeped under the function call.

5

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

u/curious_s Apr 09 '21

Thanks, that was a really clear explanation.

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

u/vattenpuss Apr 09 '21

Loops are simply obfuscated jumps.

And a tail call is just a jump.

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

u/Dwedit Apr 10 '21

Replace function call with "goto" beginning, instant tail call.