您的位置:首页 > 其它

SICP学习笔记 - 第一章 (1.3)

2012-11-02 00:23 316 查看
特殊形式(special form):

(lambda (<formal-parameters>) <body>)
(let ((<var1> <exp1>)
...
(<varn> <expn>))
<body>)


等价形式:

(define (plus4 x) (+ x 4))
等价于
(define plus4 (lambda (x) (+ x 4)))

(let ((<var1> <exp1>)
...
(<varn> <expn>))
<body>)
等价于
((lambda (<var1> ... <varn>)
<body>)
<exp1> ... <expn>)


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

(define (average x y)
(/ (+ x y) 2))
(define (close-enough? x y)
(< (abs (- x y)) 0.0001))

(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond ((positive? test-value) (search f neg-point midpoint))
((negative? test-value) (search f midpoint pos-point))
(else midpoint))))))

(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value)) (search f a b))
((and (negative? b-value) (positive? a-value)) (search f b a))
(else (error "Values are not of opposite sign." a b)))))


范例:找出函数不动点

(define (tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))


部分习题:

exercise 1.29

(define (inc x)
(+ x 1))
(define (cube x)
(* x x x))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (simpson-integral f a b n)
(define h (/ (- b a) n))
(define (y k)
(f (+ a (* h k))))
(define (term k)
(cond ((or (= k 0) (= k n)) (y k))
((even? k) (* (y k) 2))
(else (* (y k) 4))))
(* (sum term 0 inc n)
(/ h 3)))




exercise 1.30

(define (sum term a next b)
(define (sum-iter a result)
(if (> a b)
result
(sum-iter (next a) (+ result (term a)))))
(sum-iter a 0))


exercise 1.31

(define (inc x) (+ x 1))
(define (identity x) x)
; recursive product
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))

(define (factorial n)
(product identity 1 inc n))

(define (quarter-pi n)
(define (term k)
(if (even? k)
(/ (+ k 2.0) (+ k 3.0))
(/ (+ k 3.0) (+ k 2.0))))
(product term 0 inc n))

; iterative product
(define (product term a next b)
(define (product-iter a result)
(if (> a b)
result
(product-iter (next a) (* (term a) result))))
(product-iter a 1))


exercise 1.32

;recursive process
(define (accumulate combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate combiner null-value term (next a) next b))))
;iterative process
(define (accumulate combiner null-value term a next b)
(define (accumulate-iter a result)
(if (> a b)
result
(accumulate-iter (next a)
(combiner (term a) result))))
(accumulate-iter a null-value))

;sum
(define (sum term a next b)
(accumulate + 0 term a next b))
;product
(define (product term a next b)
(accumulate * 1 term a next b))


exercise 1.33

(define (accumulate-filter combiner null-value term a next b filter)
(if (> a b)
null-value
(if (filter (term a))
(combiner (term a)
(accumulate-filter combiner null-value term (next a) next b filter))
       (combiner null-value
             (accumulate-filter combiner null-value term (next a) next b filter)))))


exercise 1.34

代换模型如下:

(f f)
(f 2)
(2 2)




exercise 1.35

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


exercise 1.36

(define tolerance 0.0001)
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(newline)
(display guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))


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

(define (f)
(fixed-point (lambda (x) (average (/ (log 1000) (log x))
(log x)))
2.0))


exercise 1.37

; recursive procedure
(define (cont-frac n d k)
(define (loop index)
       (if (= index k)
        0
        (/ (n index)
          (+ (d index) (loop (+ index 1))))))
  (loop 1))

; iterative procedure
(define (cont-frac n d k)
   (define (loop-iter index result)
      (if (= index k)
        result
        (loop-iter (+ index 1)
               (/ (n index)
                 (+ (d index) result)))))
   (loop-iter 1 0))


exercise 1.38

(define (d-item k)
(cond ((= k 0) 0)
((= (remainder (+ k 1) 3) 0)
(expt 2 (/ (+ k 1) 3)))
(else 1)))
(define (euler k)
(+ (cont-frac (lambda (i) 1.0)
d-item
k)
2.0))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: