trampoline function is used to optimize a mutually recursive function relation.
This function is effective when a recursive call does not use explicit tail recursion.
The syntax is:
(trampoline f) (trampoline f & args)
Advice to coaches
The example below prints out to standard output. If the attendees are using LightTable, they need to open the console to see the result. Be careful. After trying this example on Instarepl, a response of the Instarepl may become very slow.
If we don’t use trampoline, the example below will raise “java.lang.StackOverflowError: null”.
The incrementor and decrementor functions call each other.
declare is used to define this sort of mutual call functions.
Since Clojure interprets from top to bottom, the decrementor function in incrementor function can’t be found without
To avoid StackOverflowError, we can use the
This function takes a function and the given function’s argument(s).
While the given function returns a function,
trampoline keeps calling the given function without an argument.
trampoline, we need to change incrementor and decrementor so that those will return a function. Changing it to
anonymous function does the job.
Below is a trampoline-able functions.
In the first example, we wrote functions to call each other. We can use Clojure’s core functions or macros wrapped in an anonymous function. Let’s look at the second example below. Both the right-and-left and right-and-left functions move a cursor right and left. For example:
size 3: right, right, left, done
size 4: right, right, right, left, left, right, done
size 5: right, right, right, right, left, left, left, right, right, left, done
As we saw in the first example, right-and-left function raises StackOverflowError when the size gets bigger. The right-and-left2 function that uses trampoline doesn’t.
In addition, the second example doesn’t use
Instead, it defines a local function within the