Title: Streams
1Streams
- 3.5, pages 316-352
- definitions file on web (HW.9)
1
2cons, car, cdr
- (define s
- (cons 9 (begin (display 7) 5)))
- -gt prints 7
- The display command is evaluated while
- evaluating the cons.
- (car s)
- -gt 9
- (cdr s)
- -gt 5
2
3cons-stream, stream-car, stream-cdr
- (define s
- (cons-stream 9 (begin (display 7) 5)))
- Due to the delay of the second argument,
cons-stream does not activate the display
command - (stream-car s)
- -gt 9
- (stream-cdr s)
- -gt prints 7 and returns 5
- stream-cdr activates the display which
- prints 7, and then returns 5.
3
4List enumerate
- (define (enumerate-interval low high)
- (if (gt low high) nil
- (cons low
- (enumerate-interval
- ( low 1) high))))
- (enumerate-interval 2 8)
- -gt (2 3 4 5 6 7 8)
- (car (enumerate-interval 2 8))
- -gt 2
- (cdr (enumerate-interval 2 8))
- -gt (3 4 5 6 7 8)
4
5Stream enumerate
- (define (stream-enumerate-interval low high)
- (if (gt low high) the-empty-stream
- (cons-stream low
- (stream-enumerate-interval
- ( low 1) high))))
- (stream-enumerate-interval 2 8)
- -gt (2 . ltpromisegt)
- (stream-car (stream-enumerate-interval 2 8))
- -gt 2
- (stream-cdr (stream-enumerate-interval 2 8))
- -gt (3 . ltpromisegt)
5
6List map
- (map ltprocgt ltlistgt)
- (define (map proc s)
- (if (null? s) nil
- (cons
- (proc (car s))
- (map proc (cdr s)))))
- (map square (enumerate-interval 2 8))
- -gt (4 9 16 25 36 49 64)
6
7Stream map
- (map ltprocgt ltstreamgt)
- (define (stream-map proc s)
- (if (stream-null? s) the-empty-stream
- (cons-stream
- (proc (stream-car s))
- (stream-map proc (stream-cdr s))
- )))
- (stream-map square
- (stream-enumerate-interval 2 8))
- -gt (4 . ltpromisegt)
7
8List reference
- (define (list-ref s n)
- (if ( n 0) (car s)
- (list-ref (cdr s) (- n 1))))
- (define squares
- (map square
- (enumerate-interval 2 8)))
- (list-ref squares 3)
- -gt 25
8
9Stream reference
- (define (stream-ref s n)
- (if ( n 0) (stream-car s)
- (stream-ref (stream-cdr s) (- n 1))))
- (define stream-squares
- (stream-map square
- (stream-enumerate-interval 2 8)))
- (stream-ref stream-squares 3)
- -gt 25
9
10List filter
- (filter ltpredicategt ltlistgt)
- (define (filter pred s)
- (cond
- ((null? s) nil)
- ((pred (car s))
- (cons (car s) (filter pred (cdr s))))
- (else (filter pred (cdr s)))))
- (filter even? (enumerate-interval 1 20))
- -gt (2 4 6 8 10 12 14 16 18 20)
10
11Stream filter
- (stream-filter ltpredicategt ltstreamgt)
- When will it halt?
- (define (stream-filter pred s)
- (cond
- ((stream-null? s) the-empty-stream)
- ((pred (stream-car s))
- (cons-stream
- (stream-car s)
- (stream-filter pred (stream-cdr s))))
- (else (stream-filter pred (stream-cdr s)))
- )))
- (stream-filter even? (stream-enumerate-interval
1 20)) - -gt (2 . ltpromisegt)
11
12List for each
- (define (for-each proc s)
- (if (null? s) 'done
- (begin
- (proc (car s))
- (for-each proc (cdr s)))))
12
13Stream for each
- (define (stream-for-each proc s)
- (if (stream-null? s) 'done
- (begin
- (proc (stream-car s))
- (stream-for-each proc (stream-cdr s)))))
- useful for viewing (finite!) streams
- (define (display-stream s)
- (stream-for-each display s))
- (display-stream (stream-enumerate-interval 1 20))
-gt prints 1 20 done
13
14Lists
- (define sum 0)
- (define (acc x) (set! sum ( x sum)) sum)
(define s (map acc (enumerate-interval 1 20))) s
-gt (1 3 6 10 15 21 28 36 45 55 66 78 91 105 120
136 153 171 190 210) sum -gt 210
(define y (filter even? s)) y -gt (6 10 28 36 66
78 120 136 190 210) sum -gt 210
(define z (filter (lambda (x) ( (remainder x
5) 0)) s)) z -gt (10 15 45 55 105 120 190 210)
sum -gt 210
14
15- (list-ref y 7)
- -gt 136 sum -gt 210
- (display z)
- -gt prints (10 15 45 55 105 120 190 210)
- sum -gt 210
15
16Streams
- (define sum 0)
- (define (acc x) (set! sum ( x sum)) sum)
(define s (stream-map acc (stream-enumerate-in
terval 1 20))) s -gt (1 . ltpromisegt) sum -gt 1
(define y (stream-filter even? s)) y -gt (6 .
ltpromisegt) sum -gt 6
(define z (stream-filter (lambda (x) (
(remainder x 5) 0)) s)) z -gt (10 .
ltpromisegt) sum -gt 10
16
17- (stream-ref y 7)
- -gt 136 sum -gt 136
(display-stream z) -gt prints 10 15 45 55 105 120
190 210 done sum -gt 210
17
18Infinite Streams
- Suppose we needed an infinite list of Dollars.
- We can
- (define bill-gates
- (cons-stream dollar bill-gates))
If we need a Dollar we can take the
car (stream-car bill-gates) -gt dollar
The cdr would still be an infinite list
of Dollars. (stream-cdr bill-gates)-gt(dollar .
ltpromisegt)
18
19Infinite Streams
- Formulate rules defining
- infinite series
- wishful thinking is key
19
201,1,1, ones
- 1,ones
- (define ones
- (cons-stream 1 ones))
20
212,2,2, twos
- 2,twos
- (define twos (cons-stream 2 twos))
- ones ones
- adding two infinite series of ones
- (define twos (stream-map ones ones))
- 2 ones
- element-wise operations on an infinite series of
ones - (define twos (stream-map
- (lambda (x) ( 2 x)) ones))
- or ( x x)
21
221,2,3, integers
- 1,ones integers
- 1,1,1
- 1,2,3,
- 2,3,4,
- (define integers
- (cons-stream 1
- (stream-map ones integers)))
22
230,1,1,2,3, fibs
- 0,1,fibs (fibs from 2nd position)
- 0,1,1,2,
- 1,1,2,3,
- 1,2,3,5,
- (define fibs
- (cons-stream 0
- (cons-stream 1
- (stream-map
- fibs (stream-cdr fibs)))))
23
241,2,4,8, doubles
- 1,doubles doubles
- 1,2,4,8,
- 1,2,4,8,
- 2,4,8,16,
- (define doubles (cons-stream 1
- (stream-map doubles doubles)))
24
251,2,4,8, doubles
- 1,2 doubles
- (define doubles (cons-stream 1
- (stream-map (lambda (x) ( 2 x))
doubles))) - or ( x x)
25
261,1x2,1x2x3,... factorials
- 1,factorials integers from 2nd position
- 1, 12, 123,
- 2, 3, 4,
- 12,123,1234,
- (define factorials (cons-stream 1
- (stream-map factorials
- (stream-cdr integers))))
-
x
26
27(1),(1 2),(1 2 3), runs
- (1), append runs with a list of integers from 2nd
position - (1), (1 2), (1 2 3),
- (2), (3), (4),
- (1 2),(1 2 3),(1 2 3 4),
- (define runs (cons-stream (list 1)
- (stream-map append runs
- (stream-map list
- (stream-cdr integers)))))
append
27
28a0,a0a1,a0a1a2, partial sums
- a0,partial sums (stream from 2nd pos)
- a0, a0a1, a0a1a2,
- a1, a2, a3,
- a0a1,a0a1a2,a0a1a2a3,
- (define (partial-sums a) (cons-stream
- (stream-car a)
- (stream-map
- (partial-sums a) (stream-cdr a))))
28
29Interleave
- 1,1,1,2,1,3,1,4,1,5,1,6,
- (interleave ones integers)
- s0,t0,s1,t1,s2,t2, interleave
- s0,interleave (t, s from 2nd position)
- (define (interleave s t)
- (if (stream-null? s) t
- (cons-stream
- (stream-car s)
- (interleave t (stream-cdr s)))))
29