码迷,mamicode.com
首页 > 其他好文 > 详细

SICP 1.29-1.33

时间:2016-05-01 13:32:42      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

1.29

 1 (define (sum term a next b)
 2   (if (> a b)
 3       0
 4       (+ (term a)
 5          (sum term (next a) next b))))
 6 
 7 (define (cube x)
 8   (* x x x))
 9 
10 
11 
12 (define (integral f a b n)
13   (define (helper f a b n k)
14     (define (next k)
15       (+ a (* k (/ (- b a) n))))
16     (cond ((= k n) (f (next k)))
17           ((= k 0) (+ (f (next k))
18                       (helper f a b n 1)))
19           ((= (remainder k 2) 0) (+ (* 2 (f (next k)))
20                                     (helper f a b n (+ k 1))))
21           (else (+ (* 4 (f (next k)))
22                    (helper f a b n (+ k 1))))))
23   (/ (* (helper f a b n 0) (/ (- b a) n)) 3))
24 
25 
26 
27 ----- 出现 xxxx is not applicable 的错误, 有可能是由于把函数写成了中缀表达式的情况.

 




1.30

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


1.31

---> 没必要写,和前面差不多


1.32

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



1.33

----> 类似 不写了就...

SICP 1.29-1.33

标签:

原文地址:http://www.cnblogs.com/nzhl/p/5450372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!