灯下 登录
计算机科学 / SICP / 3.5.4 Streams and Delayed Evaluation

Exercise 3.77 · 习题

Exercise 3.77: The integral procedure
used above was analogous to the “implicit” definition of the infinite stream
of integers in 3.5.2. Alternatively, we can give a definition of
integral that is more like integers-starting-from (also in
3.5.2):

(define (integral
integrand initial-value dt)
(cons-stream
initial-value
(if (stream-null? integrand)
the-empty-stream
(integral
(stream-cdr integrand)
(+ (* dt (stream-car integrand))
initial-value)
dt))))

When used in systems with loops, this procedure has the same problem as does

our original version of integral. Modify the procedure so that it

expects the integrand as a delayed argument and hence can be used in the

solve procedure shown above.

练习 3.77:上面使用的 integral 过程类似于 3.5.2 中对无穷整数流的"隐式"定义。另外,我们也可以给出一个更像 3.5.2 中 integers-starting-from 的 integral 定义:

(define (integral
integrand initial-value dt)
(cons-stream
initial-value
(if (stream-null? integrand)
the-empty-stream
(integral
(stream-cdr integrand)
(+ (* dt (stream-car integrand))
initial-value)
dt))))

当在含有循环的系统中使用时,这个过程与我们最初版本的 integral 存在同样的问题。请修改该过程,使其将被积分量 integrand 作为延迟参数接受,从而能在上面展示的 solve 过程中使用。

Racket #lang sicp
(define (integral
 integrand initial-value dt)
 (cons-stream
 initial-value
 (if (stream-null? integrand)
 the-empty-stream
 (integral
 (stream-cdr integrand)
 (+ (* dt (stream-car integrand))
 initial-value)
 dt))))