Can anyone help me better understand how to write a stream?
I understand that a stream is an infinite sequence of values and the way I have learned programming them is a representing them as a thunk that when called produces a pair of (1) the first element in the sequence and (2) a thunk that represents the stream for the second-through-infinity elements
For example:
(define powers-of-two
(letrec ([f (lambda (x) (cons x (lambda () (f (* x 2)))))])
(lambda () (f 2))))
I understand here that it is just producing a powers of two and to access these for example calling (car (powers-of-two))
would result in 2
and calling (car ((cdr (powers-of-two))))
would result in 4
Now I am trying to write a stream called red-blue
that alternates between strings red
and blue
but I am a little confused about how to construct it
For a general understanding of streams in Scheme, I'd recommend section §3.5 Streams in the SICP book. It'll teach you the basic concepts to solve stream-related problems such as the one in the question.
Regarding the problem in the question, here's the general idea to solve it:
"red"
and the other "blue"
interleave
in SICP