What Is Trampolining?
Trampolining is a pattern where a function, instead of calling the next step directly, returns a value describing that step, and a top-level loop called the trampoline repeatedly invokes the returned steps. Because each step returns before the next runs, the stack never grows with the depth of recursion. It simulates tail call optimization in environments that lack it.
Why it matters
Trampolining lets deeply recursive or mutually recursive code run safely where the runtime does not optimize tail calls. The price is some overhead and a less direct control flow.
Related guides
What Is Tail Call Optimization?Tail call optimization reuses the current stack frame for a call in tail position, so deeply recursive tail c…
What Is a Continuation?A continuation represents the rest of a computation at a given point, as a value that can be invoked later to…
What Is a Thunk?A thunk is a deferred computation wrapped as a value, evaluated only when forced, often used to implement laz…