Trace support is a nice debugging tool. When you trace a function, you make it so that each time the function is called you print out the function name and the value of its arguments. When the function finally returns, the return value is also printed out.
A scrennshot showing the result of tracing a function appears at the bottom.
Implementing a basic trace facility is quite simple and is also a nice example of higher-order programming. The tracify procedure is the heart of the system. It takes a procedure as a parameter (the procedure to trace) and returns a new procedure performing the tracing.
(define (tracify proc)
(lambda args
(display "Entering with arguments:") (write args) (newline)
(let ((result (apply proc args)))
(display "Leaving => ") (write result) (newline)
result)))
Now that you can transform a procedure into one which does tracing, you need a command/procedure to enable tracing. It turns out, you can't do that with a procedure. Think about it. What is the argument ? a procedure, but then you need to store back the traced procedure. If you pass it a symbol for the procedure's name, how do you grab the procedure associated with the symbol ?
The easy solution is to write a macro for that.
(define-syntax trace
(syntax-rules ()
((trace ?symbol) (set! ?symbol (tracify ?symbol)))))
You have now a nice tracing facility. What is missing ?