Tail-call optimization
Tail-call optimization executes an eligible call in tail position without allocating an additional call-stack frame.
Functional Programming in JavaScript
A compiler optimization that reuses the current call frame for a function call in tail position instead of creating a new stack frame.
- How it is used
- Optimizes recursive iteration when changing inputs limit the usefulness of memoization.
- How deep it goes
- Explains tail position, accumulated arguments, and single-frame reuse, but does not develop broader compiler behavior.
- Examples
- Uses factorial evaluation and accumulator-based array summation to contrast ordinary and tail-recursive forms.
- What you can do after
- Apply it by converting recursive factorial or summation functions into accumulator-based tail-recursive forms.
Read how Functional Programming in JavaScript explains tail-call optimization →
Elixir in Action, Third Edition
Tail-call optimization is a compiler or runtime optimization that reuses a caller's stack frame when its final operation is a function call.
- How it is used
- Uses tail calls to implement memory-efficient recursion and arbitrarily large iterations in Elixir.
- How deep it goes
- Explains stack-frame reuse and tail-call structure, then stops short of compiler implementation details.
- Examples
- Demonstrates natural-number printing, ordinary list summation, and accumulator-based tail-recursive list summation.
- What you can do after
- Build it from scratch using an accumulator and a final recursive call to process arbitrarily large iterations.
Read how Elixir in Action, Third Edition explains tail-call optimization →
Functional Programming in Scala, Second Edition
Tail call elimination is a compiler optimization that implements eligible recursive calls without allocating a new call-stack frame for each call.
- How it is used
- Uses tail-position recursion to express loops functionally and avoid consuming one call-stack frame per iteration.
- How deep it goes
- Defines tail position, contrasts eligible and ineligible recursion, traces factorial, and extends the idea to trampolining.
- Examples
- Demonstrates accumulator-based factorial, execution traces, deeply composed identity functions, and TailRec function composition.
- What you can do after
- Evaluate whether recursive calls are in tail position and apply TailRec trampolining when direct composition risks stack overflow.
Read how Functional Programming in Scala, Second Edition explains tail-call optimization →
Also covered in
What to know first
- recursionRecursion is a computational technique in which a definition or procedure refers to or invokes itself, directly or indirectly.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- stack overflowA stack overflow is a runtime failure caused by exhausting the available capacity of a program’s call stack.
Related concepts
- loop-recurimplemented by · In Clojure, loop/recur is a construct that re-enters a loop or function with replacement values without growing the call stack.
- tail calluses · A tail call is a function or method call that is the caller’s final operation, with its result returned directly without further computation.
- try-catch blockcontrasts with · A try-catch block is a control-flow construct that executes code under exception handling and transfers control to an associated handler when a matching exception occurs.
- recursionused by · Recursion is a computational technique in which a definition or procedure refers to or invokes itself, directly or indirectly.
- stack overflowaddresses · A stack overflow is a runtime failure caused by exhausting the available capacity of a program’s call stack.
Questions about tail-call optimization
What is tail-call optimization?
Tail-call optimization executes an eligible call in tail position without allocating an additional call-stack frame.
Which Manning books explain tail-call optimization?
Functional Programming in JavaScript, Elixir in Action, Third Edition and Functional Programming in Scala, Second Edition.
What should I know before learning tail-call optimization?
Manning books that teach tail-call optimization usually introduce recursion, loop and stack overflow first.
What is the difference between tail-call optimization and try-catch block?
Tail-call optimization executes an eligible call in tail position without allocating an additional call-stack frame. A try-catch block is a control-flow construct that executes code under exception handling and transfers control to an associated handler when a matching exception occurs.
What is tail-call optimization also called?
Tail-call optimization is also called tail-call elimination.
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.


