SICP 题解集合

1.1(略)

1.2

1 biwascheme> 
2 (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
3    (* 3 (- 6 2) (- 2 7)))
4 => -0.24666666666666667

1.3

 1 biwascheme> 
 2 (define (BiggerSum x y z)
 3     (define s1 (+ x y))
 4     (define s2 (+ x z))
 5     (define s3 (+ y z))
 6     (cond ((and (> s1 s2) (> s1 s3)) s1)
 7           ((and (> s2 s1) (> s2 s3)) s2)
 8           (else s3)
 9     )
10 )
11 biwascheme> (BiggerSum 2 1 3)
12 => 5
View Code

1.4   (略)

1.5

若解释器采用正则序求值,Ben可以看到解释器顺利返回0;

若解释器采用应用序,则程序会陷入无限循环。

1.6  (略)

1.7  

 1 (define (square x)
 2  (* x x))
 3  
 4 (define (improve guess x)
 5  (average guess (/ x guess)))
 6  
 7 (define (average x y)
 8  (/ (+ x y) 2))
 9  
10 (define (good-enough? guess0 guess1)
11  (< (abs (- (/ guess0 guess1) 1)) 0.001))
12 
13 (define (sqrt-iter guess x)
14  (define new-guess (improve guess x))
15  (if (good-enough? guess new-guess)
16     guess
17     (sqrt-iter new-guess x)))
18 (define (sqrt x)
19  (sqrt-iter 1.0 x))
View Code

 

1.8

 1 (define (square x)
 2  (* x x))
 3 (define (improve guess x)
 4  (/ (+ (/ x (square guess)) (* 2 guess)) 
 5     3)
 6 )
 7 (define (good-enough? guess0 guess1)
 8  (< (abs (- (/ guess0 guess1) 1)) 0.001))
 9 (define (cube-root-iter guess x)
10  (define new-guess (improve guess x))
11  (if (good-enough? guess new-guess)
12     guess
13     (cube-root-iter new-guess x)))
14 (define (cube-root x)
15  (cube-root-iter 1.0 x))
View Code

 

posted @ 2018-12-18 16:46  Blogggggg  阅读(244)  评论(0编辑  收藏  举报