Tail-call recursion

Knowing the terminal state has nothing to do with TCO, any more than a while loop needs to know the number of iterations in advance.

The “fib case”? Do you mean the classic fib(n) = n < 2 ? 1 : fib(n-1)+fib(n-2) recursion example? That is not tail recursive.

TCO does not mean “all recursion becomes fast”. It literally only applies to recursion that can be trivially rewritten as a loop, and hence is not needed in languages with imperative-style loops.

“call tree gets built to terminal state and then collapsed back to the root”

That sounds more like recursion unrolling, which is totally distinct from TCO and is still a research problem to make practical AFAIK.

2 Likes