Monday, October 14, 2013

Trampolining - An exercise in recursion and not blowing your stack!

This exercise was developed for the October 2013 Boston Software Craftsmanship Group Meeting


  1. Tail call optimization
  2. Tail calls vs tail recursion
  3. Continuation Passing Style & Continuations
  4. Trampoline
    1. Trampolining is a technique to implement tail-recursive optimization without consuming stack space (under the hood you're turning your recursive calls into a loop). It's commonly used in lisp compilers that compile down to C. If you implement your own trampoline you'll be able to make trampolined tail calls without blowing your stack.
    2. A trampoline is an outer function which iteratively calls an inner function. The inner function returns a thunk (or a continuation) of another function to call or the result. To turn a tail recursive function into one that can be trampolined all you need to do is return a thunk wrapping your recursive call. It's called a trampoline because it bounces from function to function. Boing, boing, boing, boing, omg I got a result.


  • Pick a stack based language of your choice (I'd recommend something with functions as first class objects)
  • Write the following two recursive functions:
  • Blow your stack!
  • Re-write to be tail recursive (you may need an accumulator)
  • Blow your stack!
  • Write a trampoline, re-write your recursive functions to operate in that world
  • No more blowing your stack!


You should come away from this exercise with a better understanding for a good number of functional programming terminology. Experience thinking about recursion and how to rewrite recursive functions as tail recursive. And maybe after we run the exercise you'll help me update this section!


"The one kind of message says, "I want to bounce: please call me again with these parameters"; the other kind says, "I've had enough bouncing: here's the final answer"."



"No assembly required : compiling Standard ML to C"
"CONS Should Not CONS Its Arguments, Part II: Cheney on the M.T.A."


Depending on your language and chosen implementation, it may be helpful to be able to check if variable is a function. In clojure you can do that easily with fn? In javascript the following code should work:
function isFunction(functionToCheck) {

 var getType = {};

 return functionToCheck && === '[object Function]';


If you need a little help see for some sample code.

No comments:

Web Statistics