SICP Exercise 1.6
If we construct a new version of if procedure, say new-if:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(new-if (= 2 3) 0 5)
5
(new-if (= 1 1) 0 5)
0
So, seems nice.
But, if we use this new-if to rewrite the square-root procedure:
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
What happens? As we know Lisp uses applictive-order evaluation(evaluate arguments and then apply) rather than normal-order(fully expand then reduce), the evaluation of these procedure sqrt-iter will firstlt evaluate the arguments of the precedure new-if. However, the third argument (sqrt-iter (improve guess x) x) is a prodecure, as a result, it's trapped into infinite recursion.

Post a Comment