Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SICP exercise 1.5 and 1.6

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?

like image 447
Vasaka Avatar asked Sep 11 '12 21:09

Vasaka


1 Answers

What happens is that the if is never reached: precisely because of the applicative order of evaluation both arguments to test get evaluated before actually calling test, 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.

like image 141
Óscar López Avatar answered Sep 21 '22 19:09

Óscar López