Problem 3: Make Some Streams (300pts in total)
Since streams only evaluate the next element when they are needed, we can combine infinite streams together for interesting results! Use it to define a few of our favorite sequences. We’ve defined the function combine-with
for you below, as well as an example of how to use it to define the stream of even numbers.
(define (combine-with f xs ys)
(if (or (null? xs) (null? ys))
nil
(cons-stream
(f (car xs) (car ys))
(combine-with f (cdr-stream xs) (cdr-stream ys)))))
scm> (define evens (combine-with + (naturals 0) (naturals 0)))
evens
scm> (slice evens 0 10)
(0 2 4 6 8 10 12 14 16 18)
For these problems, you may use the naturals
stream in addition to combine-with
. We highly recommend you do not define any other helper functions. Such problems are likely to appear on final exam.