3 (define (stream-car s) (car s))
4 (define (stream-cdr s) (force (cdr s)))
6 (define (stream-ref s n)
9 (stream-ref (stream-cdr s)
12 (define (stream-map proc . argstreams)
13 (if (stream-null? (car argstreams))
16 (apply proc (map stream-car argstreams))
18 (cons proc (map stream-cdr argstreams))))))
20 (define (scale-stream stream factor)
21 (stream-map (lambda (x) (* x factor)) stream))
24 (define (stream-map proc s)
27 (cons-stream (proc (stream-car s))
28 (stream-map proc (stream-cdr s)))))
31 (define (stream-filter pred? s)
32 (cond [(stream-null? s) the-empty-stream]
33 [(pred? (stream-car s))
34 (cons-stream (stream-car s)
35 (stream-filter pred? (stream-cdr s)))]
36 [else (stream-filter pred? (stream-cdr s))]))
38 (define (stream-for-each proc s)
43 (stream-for-each proc (stream-cdr s)))))
45 (define (display-stream s)
46 (stream-for-each display-line s))
48 (define (display-line x)
52 ;; stream-enumerate-interval
53 (define (stream-enumerate-interval low high)
57 (stream-enumerate-interval (+ low 1)
62 (define (square x) (* x x))
63 (define (smallest-divisor n)
65 (define (find-divisor n test-divisor)
66 (cond ((> (square test-divisor) n) n)
67 ((divides? test-divisor n) test-divisor)
68 (else (find-divisor n (+ test-divisor 1)))))
69 (define (divides? a b)
70 (= (remainder b a) 0))
73 (= (smallest-divisor n) n))
77 (define (integers-starting-from n)
79 (integers-starting-from (+ n 1))))
81 (define integers (integers-starting-from 1))
83 (define (add-streams s1 s2)
86 ;; integers which are not a multiple of 7
87 (define (divisible? a b) (= 0 (remainder a b)))
90 (stream-filter (lambda (x) (not (divisible? x 7)))
95 (cons-stream a (fib-gen b (+ a b))))
97 (define fibs (fib-gen 0 1))
100 (define (sieve stream)
103 (sieve (stream-filter (lambda (x)
104 (not (divisible? x (stream-car stream))))
105 (stream-cdr stream)))))
107 (define primes (sieve (integers-starting-from 2)))