The response from @ user448810 is perfect, and it will work in Racket (it uses the procedures specific to Racket). But the question is also marked by SICP , so here are my two cents.
Answering the question only with the help of a subset of the Schema procedures available in SICP, an equivalent but slightly different solution is obtained using only the following primitive stream operations defined in the book: stream-null? stream-cons stream-car stream-cdr stream-null? stream-cons stream-car stream-cdr . In particular, note that stream-map not a standard part of the Scheme, and in the book it is implemented in terms of primitive operations with an advantage over the Racket implementation - it can accept a variable number of streams as parameters:
(define (stream-map proc . args) (if (stream-null? (car args)) stream-null (stream-cons (apply proc (map stream-car args)) (apply stream-map (cons proc (map stream-cdr args)))))) (define (add-streams s1 s2) (stream-map + s1 s2))
Using the procedures described above, it is easy to define integers
(define ones (stream-cons 1 ones)) (define integers (stream-cons 1 (add-streams ones integers)))
source share