liveBook · Manning

Tail recursion

Also called tail-recursive

Tail recursion is recursion in which the recursive call is the final operation of the current function invocation.

How 4 Manning books explain it

Clojure: The Essential Reference

Tail recursion is recursion in which the recursive call is the final operation of a computation, allowing control to continue without retaining pending caller work.

How it is used
Replaces stack-consuming recursive iteration with `recur` when each next step can be the final operation.
How deep it goes
Defines tail position, specifies `recur` targets and arity, and works through Fibonacci and performance comparisons.
Examples
Uses Fibonacci, a square-collecting loop, and a powerset to contrast recursive calls with accumulating iteration.
What you can do after
Build it from scratch with `loop` and `recur`, carrying state forward while keeping recursive calls in tail position.

Read how Clojure: The Essential Reference explains tail recursion →

Functional Programming in Scala, Second Edition

Tail recursion is recursion in which the recursive call is the final computation of the calling function, allowing constant-stack evaluation when supported by the language.

How it is used
Expresses looping without mutation while retaining constant-stack evaluation when recursive calls occupy tail position.
How deep it goes
Defines tail position and demonstrates accumulator-based loops, but does not develop compiler implementation details.
Examples
Uses Fibonacci calculation, adjacent-array comparison, and random-integer list generation as concrete tail-recursive traversals.
What you can do after
Build it from scratch as Scala recursive loops using local helpers, explicit state parameters, base cases, and @annotation.tailrec.

Read how Functional Programming in Scala, Second Edition explains tail recursion →

Elixir in Action, Third Edition

Tail recursion is recursion in which each recursive call is the final operation of the current function invocation.

How it is used
Uses recursion as a memory-efficient loop for arbitrarily long iteration.
How deep it goes
Explains jump-like execution, accumulator-based implementation, termination, and why non-final calls are not tail recursive.
Examples
Demonstrates an endless loop and a tail-recursive ListHelper.sum/1 implementation over empty and nonempty lists.
What you can do after
Build it from scratch as an Elixir accumulator-based loop that handles arbitrarily large iterations.

Read how Elixir in Action, Third Edition explains tail recursion →

100 Java Mistakes and How to Avoid Them

Tail recursion is recursion in which the recursive call is the final operation performed by the method.

How it is used
Uses tail-position recursion to express recursive algorithms safely when the language or implementation can replace calls with iteration.
How deep it goes
Explains accumulators, compiler optimization, stack overflow, and mechanical loop rewriting, but does not guarantee Java optimization.
Examples
Demonstrates accumulator-based summation and recursive versus iterative string index search.
What you can do after
Apply tail recursion with accumulators, recognize optimization limits, and rewrite suitable methods as loops.

Read how 100 Java Mistakes and How to Avoid Them explains tail recursion →

Also covered in

What to know first

Related concepts

Questions about tail recursion

What is tail recursion?

Tail recursion is recursion in which the recursive call is the final operation of the current function invocation.

Which Manning books explain tail recursion?

Clojure: The Essential Reference, Functional Programming in Scala, Second Edition, Elixir in Action, Third Edition and 100 Java Mistakes and How to Avoid Them.

What should I know before learning tail recursion?

Manning books that teach tail recursion usually introduce recursion, call stack, loop and Java first.

What is the difference between tail recursion and JVM?

Tail recursion is recursion in which the recursive call is the final operation of the current function invocation. The Java Virtual Machine (JVM) is an abstract runtime environment that loads and executes Java class-file bytecode and other code compiled for its instruction set.

What is tail recursion a kind of?

Tail recursion is a kind of recursion. Recursion is a computational technique in which a definition or procedure refers to or invokes itself, directly or indirectly.

What is tail recursion also called?

Tail recursion is also called tail-recursive.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.