next up previous
Contents Next: Another Example Up: Recursive Definitions Previous: A Simple Example

Using Trace To Watch Recursion

Lisp provides a nice way to watch the recursive process using trace. Enter the following:

> (trace power)

> (power 3 4)
  1>  (POWER 3 4)          ;; Your actual output
    2>  (POWER 3 3)        ;;  may vary in format
      3>  (POWER 3 2)
        4>  (POWER 3 1)
          5>  (POWER 3 0)
          <5 (POWER 1)
        <4 (POWER 3)
      <3 (POWER 9)
    <2 (POWER 27)
  <1 (POWER 81)  
In this output, each number at the beginning of the line represents the depth of the recursion, from the top-level to the deepest call. All calls to power are documented, along with the values returned. This is a useful debugging tool to see whether your recursive function is doing what you think it should be doing.

If you redefine power and want to keep on watching the trace, then the instruction to (trace power) may have to be repeated. If you want to turn trace off, then (untrace power) will turn off trace for that specific function; (untrace) with no arguments will turn off trace for all functions you may have traced.

© Colin Allen & Maneesh Dhagat
March 2007