I've been slowly working my way though the exercises in Structure and Interpretation of Computer Programs. Section 1.1.5 talks about applicative vs. normal-order evaluation, and the topic has come up several times in the text afterward. Since the interpreter uses applicative-order evaluation, it's easy to just insert display
debug statements in your code to see exactly how it works. It would help my understanding to be able to do the same thing for normal-order evaluation.
Does anyone know of a Scheme (or Lisp) interpreter that's implemented using normal-order evaluation instead of applicative-order?
Update:
Here's a short example modified from the one given in SICP. I'll define my own add
procedure to print out the arguments, and use the square
procedure from the book.
(define (add x y)
(display x)
(display y)
(newline)
(+ x y))
(define (square x) (* x x))
Now if I run the short program (square (add 1 2))
using applicative-order evaluation, the result of (add 1 2)
will only be computed once then passed to the square
procedure. The operands 12
should be printed once before the final result. We can just run this in an interpreter to verify that this is what happens.
> (square (add 1 2))
12
9
Using normal-order evaluation, though, the single operand (add 1 2)
should be copied into the square
procedure, which would evaluate as (* (add 1 2) (add 1 2))
. The operands 12
should be printed twice before the final result.
I'd like to be able to run this in an interpreter that does normal-order evaluation to verify that this is indeed how it works.