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

    练习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))
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值