乐正

Actions speak louder than words.

Sicp-ex3-60

问题

像练习3.59里那样将幂级数表示为流之后,级数的和就可以直接用过程add-stream实现了。请完成下面级数乘积过程的定义:

1
2
(define (mul-series s1 s2)
  (cons-stream <??> (add-streams <??> <??>)))

你可以利用公式$\sin ^2 x + \cos ^2 x = 1$,用练习3.59定义的那些级数检验你定义出的过程。

解答

练习3.60 (ex3-60.scm) download
1
2
3
4
5
(define (mul-series s1 s2)
  (cons-stream (* (stream-car s1)
                  (stream-car s2))
               (add-stream (mul-series s1 (stream-cdr s2))
                           (mul-series (stream-cdr s1) s2))))

测试

1
2
3
4
5
6
7
8
(define result (add-streams (mul-series cosine-series
                                        cosine-series)
                            (mul-series sine-series
                                        sine-series)))
;Value: result

(stream-ref result 0)
;Value: 1

draft

« sicp-ex3-59 sicp-ex4-1 »

Comments