SICP 연습문제 1.6 풀이

Universal Machine/SICP | 2008/09/09 17:26 | The Doctor
이 문서는 Structure and Interpretation of Computer, by Abelson, Sussman, and Sussman의 라이센스 이용허가규약 저작자표시-비영리에 따라 제작된 문서입니다.

혼자 공부하면서 풀어본 것입니다. 틀린게 꽤 많을지도 모르겠습니다. 문제 있는 부분은 알려주시면 고맙겠습니다.

Exercise 1.6.  Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:

(define (new-if predicate then-clause else-clause)
 (cond (predicate then-clause)
       (else else-clause)))

Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)
5

(new-if (= 1 1) 0 5)
0

Delighted, Alyssa uses new-if to rewrite the square-root program:

(define (sqrt-iter guess x)
 (new-if (good-enough? guess x)
         guess
         (sqrt-iter (improve guess x)
                    x)))

What happens when Alyssa attempts to use this to compute square roots? Explain.


펼쳐두기..