In addition to question What's the explanation for Exercise 1.6 in SICP?.
So Dr. Racket (R5RS) evaluates sqrt-iter function with “if” in finite time, clearly showing normal order evaluation. But if I use example from exercise 1.5
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
(test 0 (p))
it goes into infinite loop, making me think “if” uses applicative order evaluation.
So where am I wrong?
What happens is that the
ifis never reached: precisely because of the applicative order of evaluation both arguments totestget evaluated before actually callingtest, and the expression(p)will loop forever.If the same procedure were evaluated using normal order it would return zero, that’s what this example is trying to demonstrate in the first place.