Skip to content

clojure.core/trampoline

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 5 Runnable: 3
([f] [f & args])

trampoline can be used to convert algorithms requiring mutual recursion without stack consumption. Calls f with supplied args, if any. If f returns a fn, calls that fn with no arguments, and continues to repeat, until the return value is not a fn, then returns that non-fn value. Note that if you want to return a fn as a final value, you must wrap it in some data structure and unpack it after trampoline returns.

Examples

by dakrone on
by jafingerhut on
by phreed on
by foxlog on . May have evaluation errors.
by mhmdsalem1993 on . May have evaluation errors.

Note by gstamp

A tutorial on how to use trampoline is available here:

http://pramode.net/clojure/2010/05/08/clojure-trampoline/

and here:

http://jakemccrary.com/blog/2010/12/06/trampolining-through-mutual-recursion.html

Note by dvingo

The links above are dead. Here are the archived versions:

https://web.archive.org/web/20190626232715/http://pramode.net:80/clojure/2010/05/08/clojure-trampoline/

https://web.archive.org/web/20110111121137/http://jakemccrary.com:80/blog/2010/12/06/trampolining-through-mutual-recursion.html

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.