# Yet another TCO thread

**URL:** <https://discourse.julialang.org/t/yet-another-tco-thread/81146>\
**Category:** Performance\
**Tags:** recursion\
**Created:** [May 16, 2022, 11:23am UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146 "2022-05-16T11:23:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [May 16, 2022, 11:23am UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/1 "2022-05-16T11:23:18Z")

</div>

Also, AFAIK, Julia doesn’t implement the tail-call optimization yet, which is crucial for the performance of recursive functions.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 16, 2022, 11:40am UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/2 "2022-05-16T11:40:45Z")

</div>

Any recursive algorithm that tail call elimination helps can be trivially rewritten as a loop.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 16, 2022, 12:00pm UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/3 "2022-05-16T12:00:10Z")

</div>

Tail recursion is implemented in a macro (in a package), ~~faster~~ for fib (but not really for scientific programming):

[https://github.com/JuliaLang/julia/issues/4964#issuecomment-955560668](https://github.com/JuliaLang/julia/issues/4964#issuecomment-955560668)

---

<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:** [May 16, 2022, 12:08pm UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/4 "2022-05-16T12:08:58Z")

</div>

> [@Seif\_Shebl](#):
>
> Also, AFAIK, Julia doesn’t implement the tail-call optimization yet, which is crucial for the performance of recursive functions.

Moving this to a new thread as TCO discussions tend to quickly explode, and it’s irrelevant to the original discussion of Rust benchmarks (TCO cannot be used for a function like naive `fib`).

(As Stefan says, TCO is only for “trivial” recursion that can just as easily be written as a loop. Guaranteed TCO is mainly important in functional languages where recursion is favored over loops.)

See this thread and references therein: [Does Julia have tail call optimization?](https://discourse.julialang.org/t/does-julia-have-tail-call-optimization/64101)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 16, 2022, 6:01pm UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/5 "2022-05-16T18:01:36Z")

</div>

> [@stevengj](#):
>
> TCO cannot be used for a function like naive `fib` )

To elaborate on this: the recursive calls in the `fib` benchmark are not in tail position, because you add the results, so you cannot do tail call elimination (automatically or manually).

---

<div class="post-metadata">

**Author:** ![AndrewRadcliffe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewradcliffe/32/36412_2.png) [@AndrewRadcliffe](https://discourse.julialang.org/u/AndrewRadcliffe)\
**Post date:** [May 25, 2022, 6:06am UTC](https://discourse.julialang.org/t/yet-another-tco-thread/81146/6 "2022-05-25T06:06:53Z")

</div>

> [@StefanKarpinski](#):
>
> Any recursive algorithm that tail call elimination helps can be trivially rewritten as a loop.

For folks interesting in unpacking this TCO-request-weary fellow’s terse statement, I recommend (at least) Chapter 1 of [SICP](https://web.mit.edu/6.001/6.037/sicp.pdf).

As for recursive fibonacci, p. 49 in the same helps to illustrate Stefan’s later statement.
