`
SavageGarden
  • 浏览: 215612 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SICP学习笔记 1.3.1 过程作为参数

    博客分类:
  • SICP
 
阅读更多

    练习1.29

 

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a) (sum term (next a) next b))))
      
(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))
  
(define (simpson f a b n)
  (define (add-kh k) (* k (/ (- b a) n)))
  (define (inc n) (+ n 1))
  (define (term k)
    (cond ((= k 0) (f a))
          ((= k n) (f b))
          ((= (remainder k 2) 0) (* 2.0 (f (+ a (add-kh k)))))
          (else (* 4.0 (f (+ a (add-kh k))))))) 
  (* (/ (/ (- b a) n) 3)
     (sum term 0 inc n)))
     

   > (integral cube 0 1 0.01)
   0.24998750000000042
   > (integral cube 0 1 0.001)
   0.249999875000001
   > (simpson cube 0 1 100)
   0.24999999999999992
   > (simpson cube 0 1 1000)
   0.2500000000000002

   可见使用辛普森数值积分法确实能得到更为精确的结果

 

    练习1.30

 

;; sum过程的迭代实现 
(define (sum-iter term a next b)
  (define (iter a result)
     (if (> a b)
         result
         (iter (next a) (+ (term a) result))))
  (iter a 0))

 

    练习1.31

 

a.
;; product过程的迭代实现
(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* (term a) result))))
  (iter a 1))

;; factorial过程的实现
(define (factorial n)
  (define (inc n) (+ n 1))
  (define (identity x) x)
  (product identity 1 inc n)) 
  
;; 基于product过程计算pi值
(define (pi-product n)
  (define (square x) (* x x))
  (define (term k)
    (/ (* 4.0 k (+ k 1)) (square (+ (* 2.0 k) 1))))
  (product-new term 1 inc n)) 
  
> (* 4 (pi-product 100))
3.1493784731686008
> (* 4 (pi-product 1000))
3.142377365093882
> (* 4 (pi-product 10000))
3.1416711865344946   

b.
;; product过程的递归实现
(define (product term a next b)
  (if (> a b)
      1
      (* (term a) (product term (next a) next b))))

 

    练习1.32

 

;; accumulate过程的递归实现
(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))))

;; accumulate过程的迭代实现
(define (accumulate combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (combiner (term a) result))))
  (iter a null-value))
  
;; 定义sum和product, 则需要实现其对应的combiner过程
;; combiner过程需要两个参数:当前项和前面各项累计结果, 则对sum和product分别实现如下combiner过程
(define (add x y) (+ x y))
(define (pro x y) (* x y))

;; 对应的sum和product过程为
(accumulate add 0 term a next b)
(accumulate pro 1 term a next b)
 

    练习1.33

 

;; filtered-accumulate过程的迭代实现
(define (filtered-accumulate filtered combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (if (filtered a)
            (iter (next a) (combiner (term a) result))
            (iter (next a) result))))
  (iter a null-value))
  
a)
(define (sum-prime a b)
  (define (inc n) (+ n 1))
  (define (identity x) x)
  (filtered-accumulate prime? add 0 identity a inc b))
  
b)
(define (pro-gcd i n)
  (define (inc n) (+ n 1))
  (define (identity x) x)
  (filtered-accumulate gcd? pro 1 identity i inc n))
 
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics