本节主要讲述无穷流。
3.53,显然
(define s (cons-stream 1 (add-stream s s)))
定义是2的n次方组成的无穷数列,2,4,8,16,32...
3.54,定义阶乘组成的无穷序列:
(define (mul-streams s1 s2)
(stream-map * s1 s2))
(define factorials (cons-stream 1 (mul-streams factorials (stream-cdr integers))))
3.55解答,比较有趣,也是不难的题目,列出来找出规律就成了,就是将(stream-car s)加到生成的序列中的每个元素上,通过stream-map,最后的结果就是每个元素都是前n个元素累积的结果,我的解答:
(define (partial-sums s)
(cons-stream (stream-car s) (stream-map (lambda(x) (+ x (stream-car s))) (partial-sums (stream-cdr s)))))
3.56,有了merge就好办了,根据条件合并起3种情况来就好:
(define S (cons-stream 1 (merge (scale-stream s 2) (merge (scale-stream s 3) (scale-stream s 5)))))
3.57,略过
3.58,观察到,num每次都与radix相乘并且radix保持不变,那么radix可以认为是一个基数,den也保持不变作为除数,那么这个序列就是以radix为基数对den求整数商的序列,不明白num为什么每次要变换成余数?这个序列有啥特别的用途呢?未解。
(expand 1 7 10) => 1 4 2 8 5 7 1 4 2 8 (expand 3 8 10) => 3 7 5 0 0 0 0 0 0 0
3.59解答:
a)只要将序列通过前面定义的mul-streams与整数的倒数序列相乘:
(define (integrate-series s)
(mul-streams (stream-map (lambda(x) (/ 1 x)) integers) s))
b)照着定义来了,cons的级数注意使用scale-stream乘以-1:
(define sine-series
(cons-stream 0 (integrate-series cosine-series)))
(define cosine-series
(cons-stream 1
(scale-stream
(integrate-series sine-series)
-1)))
3.64解答:
(define (stream-limit s tolerance)
(define (stream-limit-iter stream current)
(cond ((or (stream-null? stream) (null? (stream-car stream))) #f)
(else
(let ((next (stream-car stream)))
(if (< (abs (- next current)) tolerance)
next
(stream-limit-iter (stream-cdr stream) next))))))
(stream-limit-iter (stream-cdr s) (stream-car s)))
习题3.65:
(define (ln-summands n)
(cons-stream (/ 1.0 n)
(stream-map - (ln-summands (+ n 1)))))
(define ln-stream (partial-sums (ln-summands 1)))
(define ln-stream2 (euler-transform ln-stream))
(define ln-stream3 (accelerated-sequence euler-transform ln-stream))
经过欧拉变换加速过的级数收敛的很快,测测就知道
文章转自庄周梦蝶 ,原文发布时间2008-05-13