sicp-ex-3.67



<< Previous exercise (3.66) | Index | Next exercise (3.68) >>


genovia

(define (pairs s t)
  (define (top ts tt)
    (cons-stream
     (list (stream-car ts) (stream-car tt))
     (interleave
      (stream-map (lambda (x) (list (stream-car ts) x))
                  (stream-cdr tt))
      (pairs (stream-cdr ts) (stream-cdr tt)))))

  (define (below bs bt)
    (cons-stream
     (list (stream-car bs) (stream-car bt))
     (interleave
      (stream-map (lambda (x) (list (stream-car bt) x))
                  (stream-cdr bs))
      (pairs (stream-cdr bs) (stream-cdr bt)))))
  (interleave (top s t)
              (below (stream-cdr s) t)))


(define (all-pairs s t)
        (cons-stream 
                (list (stream-car s) (stream-car t))
                (interleave
                        (interleave
                                (stream-map (lambda (x) (list (stream-car s) x))
                                                        (stream-cdr t))
                                (all-pairs (stream-cdr s) (stream-cdr t)))
                        (stream-map (lambda (x) (list x (stream-car t)))
                                                (stream-cdr s)))))