Skip to content

Commit

Permalink
[序对的无穷流]
Browse files Browse the repository at this point in the history
  • Loading branch information
hjcapple committed Oct 15, 2019
1 parent a4cf406 commit 2bea6a3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@
* P235 - [练习 3.63](./exercise_3_63.md)
* P235 - [练习 3.64](./exercise_3_64.scm)
* P235 - [练习 3.65](./exercise_3_65.scm)
* P235 - [序对的无穷流](./pairs_stream.scm)




33 changes: 33 additions & 0 deletions chapter_3/pairs_stream.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#lang racket

;; P235 - [序对的无穷流]

(require "stream.scm")
(require "infinite_stream.scm")
(require "ch3support.scm")

(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)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(displayln "int-pairs")
(define int-pairs (pairs integers integers))
(display-stream-n int-pairs 20)

(displayln "prime-pairs")
(define prime-pairs (stream-filter (lambda (pair)
(prime? (+ (car pair) (cadr pair))))
int-pairs))
(display-stream-n prime-pairs 20)

0 comments on commit 2bea6a3

Please sign in to comment.