From: Ramakrishnan Muthukrishnan Date: Thu, 28 Jul 2011 16:56:24 +0000 (+0530) Subject: adding interleave and pair procedures X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=53bc477bbacacece64e1b569b68c6bbe36f65ca9;p=sicp.git adding interleave and pair procedures --- diff --git a/src/sicp/streams.rkt b/src/sicp/streams.rkt index 42bde47..b68afe1 100644 --- a/src/sicp/streams.rkt +++ b/src/sicp/streams.rkt @@ -105,3 +105,19 @@ (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))))) + +