# Tail-call recursion

**URL:** <https://discourse.julialang.org/t/tail-call-recursion/87847>\
**Category:** Performance\
**Created:** [September 26, 2022, 10:09pm UTC](https://discourse.julialang.org/t/tail-call-recursion/87847 "2022-09-26T22:09:53Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 30, 2022, 12:39am UTC](https://discourse.julialang.org/t/tail-call-recursion/87847/17 "2022-09-30T00:39:44Z")

</div>

> [@cdawg](#):
>
> If we define TCO as “loops that can be unrolled by hand because we already know the terminal state.”

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.

> [@cdawg](#):
>
> You run the calculation backward from the terminal states (x\_0 , x\_1) in fib case.

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](http://people.csail.mit.edu/rinard/paper/lcpc00.pdf), which is totally distinct from TCO and is still a research problem to make practical AFAIK.

---

_[View the full topic](https://discourse.julialang.org/t/tail-call-recursion/87847)._
