|
1 | 1 | (library
|
2 | 2 | (stream)
|
3 | 3 | (export cons-stream
|
| 4 | + list-stream |
4 | 5 | stream-car
|
5 | 6 | stream-cdr
|
6 | 7 | the-empty-stream
|
7 | 8 | stream-map
|
8 | 9 | stream-filter
|
9 |
| - stream-enumerate-interval) |
| 10 | + stream-enumerate-interval |
| 11 | + stream-display) |
10 | 12 |
|
11 |
| - (import (rnrs base)) |
| 13 | + (import (rnrs) |
| 14 | + (utils)) |
| 15 | + |
| 16 | + (define (memo-proc proc) |
| 17 | + (let ((already-run #f) (result '())) |
| 18 | + (lambda () |
| 19 | + (if (not already-run) |
| 20 | + (begin (set! result (proc)) |
| 21 | + (set! already-run #t) |
| 22 | + result) |
| 23 | + result)))) |
| 24 | + |
| 25 | + (define (force delayed-object) |
| 26 | + (delayed-object)) |
12 | 27 |
|
13 | 28 | (define-syntax cons-stream
|
14 | 29 | (syntax-rules ()
|
15 |
| - ((cons-stream a b) |
16 |
| - (cons a (lambda () b))))) |
| 30 | + ((cons-stream a b) |
| 31 | + (cons a (memo-proc (lambda () b)))))) |
| 32 | + |
| 33 | + (define-syntax list-stream |
| 34 | + (syntax-rules () |
| 35 | + [(_) the-empty-stream] |
| 36 | + [(_ a b ...) |
| 37 | + (cons-stream a |
| 38 | + (list-stream b ...))])) |
17 | 39 |
|
18 | 40 | (define (stream-car stream) (car stream))
|
19 | 41 |
|
|
24 | 46 | (define (stream-null? s)
|
25 | 47 | (null? s))
|
26 | 48 |
|
27 |
| - (define (stream-map proc s) |
28 |
| - (if (stream-null? s) |
| 49 | + ; (define (stream-map proc s) |
| 50 | + ;(if (stream-null? s) |
| 51 | + ;the-empty-stream |
| 52 | + ;(cons-stream (proc (stream-car s)) |
| 53 | + ;(stream-map proc (stream-cdr s))))) |
| 54 | + |
| 55 | + |
| 56 | + ; (stream-map proc stream stream ...) |
| 57 | + (define (stream-map proc . stream-args) |
| 58 | + (if (stream-null? (car stream-args)) |
29 | 59 | the-empty-stream
|
30 |
| - (cons-stream (proc (stream-car s)) |
31 |
| - (stream-map proc (stream-cdr s))))) |
| 60 | + (cons-stream (apply proc (map stream-car stream-args)) |
| 61 | + (apply stream-map (cons proc (map stream-cdr stream-args)))))) |
32 | 62 |
|
33 | 63 | (define (stream-filter proc s)
|
34 | 64 | (cond ((stream-null? s) the-empty-stream)
|
|
51 | 81 | (begin
|
52 | 82 | (proc (stream-car s))
|
53 | 83 | (stream-for-each proc (stream-cdr s)))))
|
| 84 | + |
| 85 | + (define (stream-display s) |
| 86 | + (if (not (stream-null? s)) |
| 87 | + (begin |
| 88 | + (println (stream-car s)) |
| 89 | + (stream-display (stream-cdr s))))) |
54 | 90 | )
|
0 commit comments