|
| 1 | +#lang racket |
| 2 | +(require "stream.rkt") |
| 3 | +(require "infinite-stream.rkt") |
| 4 | + |
| 5 | +;;; 需要混合另一个流是: (S1,T0), (S2, T0), (S3, T0), .. |
| 6 | + |
| 7 | +;; (stream-map (lambda (x) (list (stream-car s) x)) |
| 8 | +;; (stream-cdr t)) |
| 9 | +;;; 是生成 (S0, T1), (S0, T2), .. |
| 10 | + |
| 11 | +;; 那么 (S1,T0), (S2, T0), (S3, T0) 就是 |
| 12 | +;; (stream-map (lambda (x) (list x (stream-car t))) |
| 13 | +;; (stream-cdr s)) |
| 14 | + |
| 15 | +(define (full-pairs s t) |
| 16 | + (if (or (stream-null? s) (stream-null? t)) |
| 17 | + the-empty-stream |
| 18 | + (cons-stream |
| 19 | + ;; (S0,T0) |
| 20 | + (list (stream-car s) (stream-car t)) |
| 21 | + (interleave (interleave |
| 22 | + ;; (S0, T1), (S0, T2), .. |
| 23 | + (stream-map (lambda (x) (list (stream-car s) x)) |
| 24 | + (stream-cdr t)) |
| 25 | + ;; (S1,T0), (S2, T0), (S3, T0) |
| 26 | + (stream-map (lambda (x) (list x (stream-car t))) |
| 27 | + (stream-cdr s))) |
| 28 | + ;; (S1,T1), (S1, T2), (S2, T2) ... |
| 29 | + (full-pairs (stream-cdr s) (stream-cdr t)) |
| 30 | + )))) |
| 31 | + |
| 32 | +(module+ test |
| 33 | + (require rackunit) |
| 34 | + |
| 35 | + (test-case "Test for full-pairs" |
| 36 | + (define s1 (list-to-stream '(1 2 3))) |
| 37 | + (define s2 (list-to-stream '(1 2 3))) |
| 38 | + (define int-pairs (stream-to-list (full-pairs s1 s2) 9)) |
| 39 | + (check-equal? int-pairs '((1 1) (1 2) (2 2) (2 1) (2 3) (1 3) (3 3) (3 1) (3 2))) |
| 40 | + ) |
| 41 | + ) |
0 commit comments