# Is there a \`recur\` syntax to call the current function?

**URL:** <https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816>\
**Category:** General Usage\
**Tags:** syntax, design, loops, function\
**Created:** [April 18, 2020, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816 "2020-04-18T20:00:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [April 18, 2020, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816/1 "2020-04-18T20:00:21Z")

</div>

Some functional languages have this convenience. They also usually implement tail call optimizations (convert functions to while loops). This is a helpful feature to have, since it allows easier renaming of functions and makes bugs a little bit more obvious. It’s also more readable.

```julia
; Clojure example
(def factorial
  (fn [n]
    (loop [cnt n
           acc 1]
       (if (zero? cnt)
            acc
          (recur (dec cnt) (* acc cnt))
; in loop cnt will take the value (dec cnt)
; and acc will take the value (* acc cnt)
))))

```

---

<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:** [April 18, 2020, 9:23pm UTC](https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816/2 "2020-04-18T21:23:32Z")

</div>

The main reason, as I understand it, for `recur` is that it refers to calling the same _method_ that it occurs within. That means that you’re guaranteed that the recursion can be transformed into an equivalent iterative definition. Otherwise, a call to the same function might actually dispatch to a different method. So it’s a limitation, rather than a convenience. It would be possible, but it seems like expressing recursion by calling the function is better and more expressive (although harder to do TCO on).

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [April 19, 2020, 12:44am UTC](https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816/3 "2020-04-19T00:44:17Z")

</div>

IIUC, Clojure has to workaround the problem that JVM does not have TCO. So I guess it makes sense for Clojure to use `recur`?

In Julia, I think recurring to the same method makes less sense as the plain `for` syntax is more idiomatic way to write loops. But it’d be nice to have a syntax for tail-call into different method since, in principle, the compiler can build a finite state machine out of this. This would be more powerful tool than union splitting when writing generic reductions: [Tail-call optimization and function-barrier -based accumulation in loops](https://discourse.julialang.org/t/tail-call-optimization-and-function-barrier-based-accumulation-in-loops/25831)

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [April 19, 2020, 10:03am UTC](https://discourse.julialang.org/t/is-there-a-recur-syntax-to-call-the-current-function/37816/4 "2020-04-19T10:03:11Z")

</div>

I fail to see how one can write elegant recursions using `for`. How do you implement a binary search with `for`? It seems to me that this needs an ugly `while true` that manually recurs.
