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 ...

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions