SICP Exercise 3.53
Exercise 3.53. Without running the program, describe the elements of the stream defined by
(define s (cons-stream 1 (add-streams s s)))
SOLUTION
The explanation and tests are here.
EXPLANATION
(add-streams s s) produces a stream the first element of which is 1 + 1 = 2. So s produces:
1, 2 ...
The next element in the stream will be the addition of the next element of s with itself
i.e. 2 + 2 = 4. So s will be
1, 2, 4 ...
So the elements of s will be:
1, 2, 4, 8, 16, 32 ...
(define s (cons-stream 1 (add-streams s s)))
SOLUTION
The explanation and tests are here.
EXPLANATION
(add-streams s s) produces a stream the first element of which is 1 + 1 = 2. So s produces:
1, 2 ...
The next element in the stream will be the addition of the next element of s with itself
i.e. 2 + 2 = 4. So s will be
1, 2, 4 ...
So the elements of s will be:
1, 2, 4, 8, 16, 32 ...
Comments
Post a Comment