Exercise 1.1
10
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
b
a)
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
(+ 2 (if(> b a) b a))
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
Exercise 1.2
(/ (+ 5 4 (- 2 (- 3 (+ 6 4/5))))(* 3 (- 6 2)(- 2 7)))
Exercise 1.3
(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sss x y z)
(cond ((and (> x z) (> y z)) (sum-of-squares x y))
((and (> y x) (> z x)) (sum-of-squares y z))
((and (> x y) (> z y)) (sum-of-squares x z))
))
(sss 4 3 4)
Exercise 1.5
applicative-order에서는 (test 0 (p))가 절대 안끝난다. 왜냐면 (p)가 무한대로 불러지기 때문이다.
normal-order evaluation에서는 표현의 평가가 차례로 이뤄지므로 결과가 나온다.
Exercise 1.6
무한루프에 빠짐. Scheme은 applicative 방식이라 operand를 먼저 평가하기 때문임.
Exercise 1.7
(define (new-good-enough? guess x)
(< (abs (- guess (improve guess x))) 0.0000001))
Exercise 1.8
(define (cube-improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess))
3))
(cube-improve 3 27)
(define (cube-good-enough? guess x)
(< (abs (- guess (cube-improve guess x))) 0.0000001))
(define (cube-iter guess x)
(if (cube-good-enough? guess x)
guess
(cube-iter (cube-improve guess x)
x)))
(define (cube x)
(cube-iter 1.0 x))
(cube 64.0)
Exercise 1.9
(+ 4 5)
(inc (+ (dec 4) 5))
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9
(+ 4 5)
(+ (dec 4) (inc 5))
(+ 3 6)
(+ (dec 3) (inc 6))
(+ (dec 2) (inc 7))
(+ (dec 1) (inc 8))
(+ 0 9)
(9)
Exercise 1.10
(f n): 2n
(g n): 2^{n}
(h n): 2^2^… (n-1 times)
'코딩 > SICP' 카테고리의 다른 글
SICP(Structure and Interpretation of Computer Programs) Exercise 1.11~1.20 (0) | 2016.07.22 |
---|