Version 3 of Trampoline

Updated 2005-04-09 10:10:22 by suchenwi

Richard Suchenwirth 2005-04-09 - A trampoline is a software device to repeatedly call some code until a terminal condition is met. It can give the effect of recursion without consuming much stack. Slightly rewritten from Playing TRAC, here is a simple trampoline:

 proc trampoline form {
    while 1 {
      set new [subst $form]
       if {$new eq $form} break
       set form $new
    set form

#-- Testing with the classic factorial - either constant 1 is returned, or a bracketed expression for the next trampoline jump:

 proc fac n {
    expr {$n<=1? 1: "\[expr $n * \[fac [incr n -1]]]"}
 % fac 5
 [expr 5 * [fac 4]]
 % trampoline {[fac 5]}

In the dialect developed in Playing TRAC, factorial looks like this:

 DS fac {[GR $1 1 "\[ML $1 \[CL fac [SU $1 1]]]" 1]}

The structural similarity should be evident.

See also Tail call optimization | Tail call optimisation (both have their merits :)

Category Concept | Arts and crafts of Tcl-Tk programming