]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/streams.rkt
adding interleave and pair procedures
[sicp.git] / src / sicp / streams.rkt
index 42bde472c9a91079104933978dbba739a8f67d0f..b68afe1ff0636c15a75de12e8d9192f788dadc9f 100644 (file)
                          (stream-cdr stream)))))
                
 (define primes (sieve (integers-starting-from 2)))
+
+(define (interleave s1 s2)
+  (if (stream-null? s1)
+      s2
+      (cons-stream (stream-car s1)
+                   (interleave s2 (stream-cdr s1)))))
+
+(define (pairs s t)
+  (cons-stream
+   (list (stream-car s) (stream-car t))
+   (interleave
+    (stream-map (lambda (x) (list (stream-car s) x))
+                (stream-cdr t))
+    (pairs (stream-cdr s) (stream-cdr t)))))
+
+