SICP学习笔记 第一章 (1.3)

特殊形式(special form):

1 (lambda (<formal-parameters>) <body>)
2 (let ((<var1> <exp1>)
3       ...
4       (<varn> <expn>))
5     <body>)

等价形式:

 1 (define (plus4 x) (+ x 4))
 2 等价于
 3 (define plus4 (lambda (x) (+ x 4)))
 4 
 5 (let ((<var1> <exp1>)
 6         ...
 7       (<varn> <expn>))
 8     <body>)
 9 等价于
10 ((lambda (<var1> ... <varn>)
11     <body>)
12     <exp1> ... <expn>)

范例:区间折半法找方程的根

 1 (define (average x y)
 2     (/ (+ x y) 2))
 3 (define (close-enough? x y)
 4     (< (abs (- x y)) 0.0001))
 5 
 6 (define (search f neg-point pos-point)
 7     (let ((midpoint (average neg-point pos-point)))
 8         (if (close-enough? neg-point pos-point)
 9             midpoint
10             (let ((test-value (f midpoint)))
11                 (cond ((positive? test-value) (search f neg-point midpoint))
12                       ((negative? test-value) (search f midpoint pos-point))
13                       (else midpoint))))))
14 
15 (define (half-interval-method f a b)
16     (let ((a-value (f a))
17           (b-value (f b)))
18         (cond ((and (negative? a-value) (positive? b-value)) (search f a b))        
19               ((and (negative? b-value) (positive? a-value)) (search f b a))
20               (else (error "Values are not of opposite sign." a b)))))

 范例:找出函数不动点

 1 (define (tolerance 0.00001)
 2 (define (fixed-point f first-guess)
 3     (define (close-enough? v1 v2)
 4         (< (abs (- v1 v2)) tolerance))
 5     (define (try guess)
 6         (let ((next (f guess)))
 7             (if (close-enough? guess next)
 8                 next
 9                 (try next))))
10     (try first-guess))

部分习题:

exercise 1.29

 1 (define (inc x)
 2     (+ x 1))
 3 (define (cube x)
 4     (* x x x))
 5 (define (sum term a next b)
 6     (if (> a b)
 7         0
 8         (+ (term a)
 9            (sum term (next a) next b))))
10 (define (simpson-integral f a b n)
11     (define h (/ (- b a) n))
12     (define (y k)
13         (f (+ a (* h k))))
14     (define (term k)
15         (cond ((or (= k 0) (= k n)) (y k))
16               ((even? k) (* (y k) 2))
17               (else (* (y k) 4))))
18     (* (sum term 0 inc n)
19         (/ h 3)))

exercise 1.30

1 (define (sum term a next b)
2     (define (sum-iter a result)
3         (if (> a b)
4             result
5             (sum-iter (next a) (+ result (term a)))))
6     (sum-iter a 0))

exercise 1.31

 1 (define (inc x) (+ x 1))
 2 (define (identity x) x)
 3 ; recursive product
 4 (define (product term a next b)
 5     (if (> a b)
 6         1
 7         (* (term a)
 8            (product term (next a) next b))))
 9 
10 (define (factorial n)
11     (product identity 1 inc n))
12 
13 (define (quarter-pi n)
14     (define (term k)
15         (if (even? k)
16             (/ (+ k 2.0) (+ k 3.0))
17             (/ (+ k 3.0) (+ k 2.0))))
18     (product term 0 inc n))
19 
20 ; iterative product
21 (define (product term a next b)
22     (define (product-iter a result)
23         (if (> a b)
24             result
25             (product-iter (next a) (* (term a) result))))
26     (product-iter a 1))

 exercise 1.32

 1 ;recursive process
 2 (define (accumulate combiner null-value term a next b)
 3     (if (> a b)
 4         null-value
 5         (combiner (term a)
 6                   (accumulate combiner null-value term (next a) next b))))
 7 ;iterative process
 8 (define (accumulate combiner null-value term a next b)
 9     (define (accumulate-iter a result)
10         (if (> a b)
11             result
12             (accumulate-iter (next a)
13                              (combiner (term a) result))))
14     (accumulate-iter a null-value))
15 
16 ;sum
17 (define (sum term a next b)
18     (accumulate + 0 term a next b))
19 ;product
20 (define (product term a next b)
21     (accumulate * 1 term a next b))

 exercise 1.33

1 (define (accumulate-filter combiner null-value term a next b filter)
2     (if (> a b)
3         null-value
4         (if (filter (term a))
5             (combiner (term a)
6                       (accumulate-filter combiner null-value term (next a) next b filter))
7        (combiner null-value
8              (accumulate-filter combiner null-value term (next a) next b filter)))))

exercise 1.34

代换模型如下:

1 (f f)
2 (f 2)
3 (2 2)

exercise 1.35

1 (define (golden-ratio)
2     (fixed-point (lambda (x) (+ 1 (/ 1 x)))
3                  1.0))

exercise 1.36

 1 (define tolerance 0.0001)
 2 (define (fixed-point f first-guess)
 3     (define (close-enough? v1 v2)
 4         (< (abs (- v1 v2)) tolerance))
 5     (define (try guess)
 6         (newline)
 7         (display guess)
 8         (let ((next (f guess)))
 9             (if (close-enough? guess next)
10                 next
11                 (try next))))
12     (try first-guess))

 设初始猜测值为 x1 且x1 != 1。则x2 = log(1000) / log(x1),x3 = log(x1),可以看到猜测值不停在 log(1000) / log(x1) 和 log(x1) 之间往复。

1 (define (f)
2     (fixed-point (lambda (x) (average (/ (log 1000) (log x))
3                                       (log x)))
4                  2.0))

 exercise 1.37

 1 ; recursive procedure
 2 (define (cont-frac n d k)
 3     (define (loop index)
 4        (if (= index k)
 5         0
 6         (/ (n index)
 7           (+ (d index) (loop (+ index 1))))))
 8   (loop 1))
 9 
10 
11 ; iterative procedure
12 (define (cont-frac n d k)
13    (define (loop-iter index result)
14       (if (= index k)
15         result
16         (loop-iter (+ index 1)
17                (/ (n index)
18                  (+ (d index) result)))))
19    (loop-iter 1 0))

exercise 1.38

 1 (define (d-item k)
 2     (cond ((= k 0) 0)
 3            ((= (remainder (+ k 1) 3) 0) 
 4             (expt 2 (/ (+ k 1) 3)))
 5            (else 1)))
 6 (define (euler k)
 7     (+ (cont-frac (lambda (i) 1.0)
 8                   d-item
 9                   k)
10         2.0))
原文地址:https://www.cnblogs.com/sungoshawk/p/2749597.html