Вопрос

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 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.

Другие советы

Using DrRacket with a #lang scheme directive in the first line, I tried it this way ...

;say "hi" to Ben Bitdiddle

(define (q) (display "Hello?...Ben?)

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

Then I used the DrRacket 5.3 debug tool to step through the evaluation.
It's clearly applicative-order evaluation.

Eval window shows -> Hello? ... Ben? followed by a 0 cuz DrRacket evaluates the call to (q) before entering the "if".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top