-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.scm
90 lines (75 loc) · 2.45 KB
/
streams.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#lang sicp
(define (memo-proc proc)
(let ((already-run? false) (result false))
(lambda ()
(if (not already-run?)
(begin (set! result (proc))
(set! already-run? true)
result)
result))))
; (define (delay exp) (memo-proc (lambda () exp)))
; (define (force delayed-object)
; (delayed-object))
(define (stream-cons a b) (cons a (delay b)))
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
(define stream-null? null?)
(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))
; (define (stream-map proc s)
; (if (stream-null? s)
; the-empty-stream
; (cons-stream (proc (stream-car s))
; (stream-map proc (stream-cdr s)))))
(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each proc (stream-cdr s)))))
(define (display-stream s)
(stream-for-each display-line s))
(define (display-line x)
(newline)
(display x))
(define (stream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(stream-cons
(apply proc (map stream-car argstreams))
(apply stream-map
(cons proc (map stream-cdr argstreams))))))
; (display-stream (stream-map + (stream-cons 1 (stream-cons 2 '())) (stream-cons 30 (stream-cons 40 '())) (stream-cons 500 (stream-cons 600 '()))))
(define (stream-enumerate-interval low high)
(if (> low high)
the-empty-stream
(stream-cons
low
(stream-enumerate-interval (+ low 1) high))))
(define (stream-filter pred stream)
(cond ((stream-null? stream) the-empty-stream)
((pred (stream-car stream))
(stream-cons (stream-car stream)
(stream-filter pred
(stream-cdr stream))))
(else (stream-filter pred (stream-cdr stream)))))
(define (show x)
(display-line x)
x)
; (define x (stream-map show (stream-enumerate-interval 0 10)))
; (define x (stream-enumerate-interval 0 10))
; x
; (stream-ref x 5)
; (stream-ref x 7)
; Exercise 3.52.
; (define sum 0)
; (define (accum x)
; (set! sum (+ x sum))
; sum)
; (define seq (stream-map accum (stream-enumerate-interval 1 20)))
; (define y (stream-filter even? seq))
; (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
; seq))
; (stream-ref y 7)
; (display-stream z)