Chapter 3, Modularity, Objects, and State

Exercise 3.55


Denoting the terms of the series as and denoting the series from which we generate as . Then the series can be defined as:

Now, its simple to write the procedure:

1
2
(define (partial-sums stream)
  (cons-stream (stream-car stream) (add-streams (stream-cdr stream) (partial-sums stream))))

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1 ]=> (define S (partial-sums integers))

;Value: s

1 ]=> (stream-ref S 0)

;Value: 1

1 ]=> (stream-ref S 1)

;Value: 3

1 ]=> (stream-ref S 2)

;Value: 6

1 ]=> (stream-ref S 3)

;Value: 10

1 ]=> (stream-ref S 4)

;Value: 15

1 ]=> (stream-ref S 5)

;Value: 21

1 ]=> (stream-ref S 6)

;Value: 28