7

One must decide, based on the value of:

(test 0 (p))

where test is defined as :

(define (test x y)
  (if (= x 0)
      0
      y))

and p is defined as :

(define (p) (p))

When I evaluate (test 0 (p)) the interpreter goes into an infinite loop, suggesting that it is evaluating p. This shows normal-order evaluation, because the operands are evaluated before being substituted for parameters. SICP says LISP uses applicative-order evaluation.

Geoffrey
  • 5,407
  • 10
  • 43
  • 78
  • 2
    A helpful tip from later in the text: the reason it's called "applicative-order" is that the operands are evaluated before the operator is **applied**. There's a bit more discussion on "lazy" evaluation etc in chapter 3, and I absolutely could not keep the two straight until I read that, they really should have explained it sooner than chapter 4. – okonomichiyaki Dec 14 '10 at 15:03

1 Answers1

15

This shows normal-order evaluation, because the operands are evaluated before being substituted for parameters

Actually you got it the wrong way around. Applicative order is when the operands are evaluated first. Normal-order is when the arguments are substituted into the expression unevaluated.

So racket uses applicative order because as you said the arguments are evaluated first (unless you use "Lazy Racket" in which case it uses call-by-need aka lazy evaluation, which is like normal-order except each argument is evaluated at most once).

sepp2k
  • 363,768
  • 54
  • 674
  • 675