← Back to context

Comment by kazinator

13 hours ago

Tail recursive functions are loops when tail calls are jumps.

Tail calls being jumps is the key insight.

Not all tail calls are recursion!

Students are confused by recursion. Some may be confused by tail calls.

Don't mix two confusing things together when teaching.

(Tail calls get mixed up with recursion because recognizing and treating/optimizing recursive tail calls to create loops an important highlighted category. In a compiler that doesn't optimize tail calls, optimizing self tail calls is an important priority (if that is easier to do than a general treatment). Many common algorithms are expressible in just one function that calls itself, and there are cases in which some or all of those calls can be tail calls; e.g. binary tree traversal.)

The order should probably be: teach recursion first. Then when students have firmly wrapped their heads around it, introduce tail calls. Students will gain the skill of knowing what is a tail call and why it is important, and recognize which calls tail calls. Secondly, the harder skill of taking recursion that uses non-tail calls and restructuring it to make tail calls instead.