Browse Learn Clojure Foundations as a Java Developer

Tail Recursion with recur

Use Clojure's explicit recur form for stack-safe tail-position loops, and understand when ordinary recursion, reduce, or sequence functions are clearer.

Plain recursion uses the call stack. If you recursively call yourself for each element of a large collection, you can blow the stack.

Clojure provides recur as an explicit, safe way to express tail recursion. When you use recur in the correct position, it compiles to a loop without growing the call stack.

The rules you need to remember

  • recur can only appear in tail position (the final action).
  • recur can target the nearest loop or the current function.
  • If the compiler accepts it, it is stack-safe.

Java mental model: recur is the closest equivalent to a while loop, but expressed in a functional style with explicit state variables.

In this section

  • How Tail Recursion Works in Clojure
    Understand tail position, accumulator state, and why Clojure requires explicit recur instead of automatically optimizing ordinary recursive calls.
  • Using recur in Clojure
    Rewrite loop-shaped recursive code with loop/recur, accumulators, and tail-position updates that Java engineers can review for stack safety.
  • Limits of recur in Clojure
    Learn the two hard recur rules: it must be in tail position, and it can only target the nearest function or loop.
Revised on Saturday, May 23, 2026