Outline Streams Infinite streams Stream implementation Questions from
- Slides: 85
Outline • • Streams Infinite streams Stream implementation Questions from exams 2
Streams 3. 5, pages 316 -352 Definitions file on web
cons, car, cdr (define s (cons 9 (begin (display 7) 5))) -> prints 7 The display command is evaluated while evaluating the cons. (car s) -> 9 (cdr s) -> 5 4
cons-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) -> 9 (stream-cdr s) -> prints 7 and returns 5 stream-cdr activates the display which prints 7, and then returns 5. 5
List enumerate (define (enumerate-interval low high) (if (> low high) nil (cons low (enumerate-interval (+ low 1) high)))) (enumerate-interval 2 8) -> (2 3 4 5 6 7 8) (car (enumerate-interval 2 8)) -> 2 (cdr (enumerate-interval 2 8)) -> (3 4 5 6 7 8) 6
Stream enumerate (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (stream-enumerate-interval 2 8) -> (2. #<promise>) (stream-car (stream-enumerate-interval 2 8)) -> 2 (stream-cdr (stream-enumerate-interval 2 8)) -> (3. #<promise>) 7
List map (map <proc> <list>) (define (map proc s) (if (null? s) nil (cons (proc (car s)) (map proc (cdr s))))) (map square (enumerate-interval 2 8)) -> (4 9 16 25 36 49 64) 8
Stream map (map <proc> <stream>) (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)) -> (4. #<promise>) 9
List of squares (define squares (map square (enumerate-interval 2 8))) squares -> (4 9 16 25 36 49 64) (car squares) -> 4 (cdr squares) -> (9 16 25 36 49 64) 10
Stream of squares (define stream-squares (stream-map square (stream-enumerate-interval 2 8))) stream-squares -> (4. #<promise>) (stream-car stream-squares) -> 4 (stream-cdr stream-squares) -> (9. #<promise>) 11
List 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) -> 25 12
Stream 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) -> 25 13
List filter (filter <predicate> <list>) (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)) -> (2 4 6 8 10 12 14 16 18 20) 14
Stream filter (stream-filter <predicate> <stream>) (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)) 15 -> (2. #<promise>)
Generalized list map (generalized-map <proc> <list 1> … <listn>) (define (generalized-map proc. arglists) (if (null? (car arglists)) nil (cons (apply proc (map car arglists)) (apply generalized-map (cons proc (map cdr arglists)))))) (generalized-map + squares) -> (12 27 48 75 108 147 192) 16
Generalized stream map (generalized-stream-map <proc> <stream 1> … <streamn>) (define (generalized-stream-map proc. argstreams) (if (stream-null? (car argstreams)) the-empty-stream (cons-stream (apply proc (map stream-car argstreams)) (apply generalized-stream-map (cons proc (map stream-cdr argstreams)))))) (generalized-stream-map + stream-squares) -> (12. #<promise>) 17
List for each (define (for-each proc s) (if (null? s) 'done (begin (proc (car s)) (for-each proc (cdr s))))) 18
Stream 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 19 20)) -> prints 1 … 20 done
Lists (define sum 0) (define (acc x) (set! sum (+ x sum)) sum) (define s (map acc (enumerate-interval 1 20))) s -> (1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210) sum -> 210 (define y (filter even? s)) y -> (6 10 28 36 66 78 120 136 190 210) sum -> 210 (define z (filter (lambda (x) (= (remainder x 5) 0)) s)) z -> (10 15 45 55 105 120 190 210) sum -> 210 20
(list-ref y 7) -> 136 sum -> 210 (display z) -> prints (10 15 45 55 105 120 190 210) sum -> 210 21
Streams (define sum 0) (define (acc x) (set! sum (+ x sum)) sum) (define s (stream-map acc (stream-enumerate-interval 1 20))) s -> (1. #<promise>) sum -> 1 (define y (stream-filter even? s)) y -> (6. #<promise>) sum -> 6 (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) s)) z -> (10. #<promise>) sum -> 10 22
(stream-ref y 7) -> 136 sum -> 136 (display-stream z) -> prints 10 15 45 55 105 120 190 210 done sum -> 210 23
Defining streams implicitly by delayed evaluation 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) -> dollar The cdr would still be an infinite list of Dollars. (stream-cdr bill-gates)->(dollar. #<promise>) 24
Infinite Streams Formulate rules defining infinite series wishful thinking is key 25
1, 1, 1, … = ones = 1, ones (define ones (cons-stream 1 ones)) 26
2, 2, 2, … = twos = 2, twos (define twos (cons-stream 2 twos)) ones + ones adding two infinite series of ones (define twos (stream-map + ones)) 2 * ones element-wise operations on an infinite series of ones (define twos (stream-map (lambda (x) (* 2 x)) ones)) or (+ x x) 27
1, 2, 3, … = integers = 1, ones + integers 1, 1, 1… + 1, 2, 3, … 2, 3, 4, … (define integers (cons-stream 1 (stream-map + ones integers))) 28
0, 1, 1, 2, 3, … = fibs = 0, 1, fibs + (fibs from 2 nd 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))))) 29
1, 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))) 30
1, 2, 4, 8, … = doubles = 1, 2 * doubles (define doubles (cons-stream 1 (stream-map (lambda (x) (* 2 x)) doubles))) or (+ x x) 31
1, 1 x 2 x 3, . . . = factorials = 1, factorials * integers from 2 nd position 1, 1*2*3, … x 2, 3, 4, … 1*2, 1*2*3*4, … (define factorials (cons-stream 1 (stream-map * factorials (stream-cdr integers)))) 32
(1), (1 2 3), … = runs = (1), append runs with a list of integers from 2 nd position (1), (1 2 3), … append (2), (3), (4), … (1 2), (1 2 3 4), … (define runs (cons-stream (list 1) (stream-map append runs (stream-map list (stream-cdr integers))))) 33
a 0, a 0+a 1+a 2, … = partial sums = a 0, partial sums + (stream from 2 nd pos) a 0, a 0+a 1+a 2, … + a 1, a 2, a 3, … a 0+a 1, a 0+a 1+a 2+a 3, … (define (partial-sums a) (cons-stream (stream-car a) (stream-map + (partial-sums a) (stream-cdr a)))) 34
Partial Sums (cont. ) (define (partial-sums a) (define sums (cons-stream (stream-car a) (stream-map + sums (stream-cdr a)))) sums) This implementation is more efficient since it uses the stream itself rather than recreating it recursively 35
Repeat Input: procedure f of one argument, number of repetitions Output: f*…*f, n times (define (repeated f n) (if (= n 1) f (compose f (repeated f (- n 1))))) (define (compose f g) (lambda (x) (f (g x)))) 36
Repeat stream f, f*f*f, … = repeat = f, compose f, f, f, … with repeat (define f-series (cons-stream f f-series)) (define stream-repeat (cons-stream f (stream-map compose f-series stream-repeat))) We would like f to be a parameter 37
Repeat stream f, f*f*f, … = repeat = f, compose f, f, f, … with repeat (define (make-repeated f) (define f-series (cons-stream f f-series)) (define stream-repeat (cons-stream f (stream-map compose f-series stream-repeat))) stream-repeat) 38
Interleave 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, … (interleave ones integers) s 0, t 0, s 1, t 1, s 2, t 2, … interleave = s 0, interleave (t, s from 2 nd position) (define (interleave s t) (if (stream-null? s) t (cons-stream (stream-car s) (interleave t (stream-cdr s))))) 39
streams מימוש
stream מימוש force/delay • מנגנוני special form הוא delay • (delay <exp>) => (lambda () <exp>) (force <delayed-exp>) => ((<delayed-exp>)) => ((lambda () <exp>)) => <exp> 41
stream מימוש memoization • (define (memo-proc) (let ((already-run? #f) (result #f)) (lambda () (if (not already-run? ) (begin (set! result (proc)) (set! already-run? #t) result)))) 42
stream מימוש : delay • הגדרה מתוקנת עבור (delay <exp>) => (memo-proc (lambda () <exp>)) : • ולכן (cons-stream a b) => (cons a (delay b)) 43
streams GE x: 0 (define ones (cons-stream 1 (begin (set! x (+ x 1)) ones)) 44
streams GE x: 0 (define ones (cons 1 45 (delay (begin (set! x (+ x 1)) ones)))
streams GE x: 0 (define ones (cons 1 (memo-proc(lambda (). . . ))) 46
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (define ones (cons 1 (memo-proc(lambda (). . . ))) 47
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (stream-cdr ones) 48
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (force (cdr ones)) 49
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … ((cdr ones)) 50
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (not already-run) is #t => (set! result (proc)) 51
streams x: 0 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (begin (set! x (+ x 1)) ones) 52
streams / x: 01 ones: GE proc: 1 already-run: #f result: #f p: b: (begin…) p: b: … (begin (set! x (+ x 1)) ones) 53
streams / x: 01 ones: GE proc: 1 already-run: #f #t result: #f p: b: (begin…) p: b: … (set! already-run #t) 54
streams / x: 01 ones: GE proc: 1 already-run: #f #t result: #f p: b: (begin…) p: b: … (stream-cdr ones) 55
streams / x: 01 ones: GE proc: 1 already-run: #f #t result: #f p: b: (begin…) p: b: … ((cdr ones)) 56
streams / x: 01 ones: GE proc: 1 already-run: #f #t result: #f p: b: (begin…) p: b: … (not already-run) is #f => result 57
streams / x: 01 ones: GE proc: 1 already-run: #f #t result: #f p: b: (begin…) p: b: … 58
Questions from Exams Extra
convert-all (define (convert-all numbers bases) (map _________________ (lambda (base) _________________ (stream-map _________________ (lambda (x) (convert x base)) _________________ numbers)) _________________ bases)) _________________ ) 62
common-digit-all (define (common-digit-all nums bases) (map (lambda (base) _________________ (stream-map (lambda (n 1 n 2) _________________ (common-digit? (convert n 1 base) _________________ (convert n 2 base))) _________________ (stream-cdr numbers) _________________ numbers)) _________________ bases)) _________________ ) 64
in-sorted? (define in-sorted? (lambda (x stream) stream-car (let ((e (_____ stream))) (or (= x e) and > (______ (____ x e) in-sorted? (_____ x stream-cdr (_____ stream)) ))))) 66
merge-inc (define (merge-inc s 1 s 2) (let ((e 1 (_____ s 1)) stream-car (e 2 (_____ s 2))) stream-car (if (___ e 1 e 2) < (______ e 1 cons-stream (merge-inc (_____ s 1) s 2)) stream-cdr (______ e 2 cons-stream (merge-inc s 1 (_____ s 2)) stream-cdr ))))) 68
stream-conv (define (stream-conv lst str) (define (first-term lst str) 0 (if (null? lst) ___ + (___ (* (car lst) stream-car (_____ str)) cdr (first-term (_____ lst) stream-cdr (_____ str) )))) cons-stream (______ (first-term lst str) stream-cdr (stream-conv lst (_____ str)) )) 70
stream-conv ? בסוף השערוך x 2 - ו x 1 • מהם ערכי / 1 (define x 1 0) / 2 (define x 2 0) (define str 1 (cons-stream 1 (begin (set! x 1 (+ x 1 1)) str 1))) (define (integers n) (cons-stream n (begin (set! x 2 (+ x 2 1)) (integers (+ n 1))))) (define str 2 (integers 1)) (define c 1 (stream-conv '(1 1) str 1)) (define c 2 (stream-conv '(1 1) str 2)) 71
מודל הסביבות GE L 1 p: f b: (define y … ((lambda (f). . . ) (lambda (x) (+ x 3) 74
מודל הסביבות GE L 2 L 1 p: f p: x b: (+ x 3) b: (define y … (L 1 (lambda (x) (+ x 3) 75
מודל הסביבות GE E 1 L 2 L 1 f: p: f p: x b: (+ x 3) b: (define y … (L 1 L 2) 77
מודל הסביבות GE E 1 L 2 L 1 f: y: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (lambda…) (define y (lambda (g) (lambda (y) (f (g y))))) | E 1 78
מודל הסביבות GE E 1 L 2 L 1 f: y: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (lambda…) ((y f) 3) | E 1 79
מודל הסביבות GE E 1 L 2 L 1 f: y: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (lambda…) ((L 3 f) 3) | E 1 80
מודל הסביבות GE E 1 L 2 L 1 f: y: E 2 g: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (lambda (y) (f (g y))) ((L 3 f) 3) | E 1 81
מודל הסביבות GE E 1 L 2 L 1 f: y: E 2 g: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (lambda (y) (f (g y))) (lambda (y) (f (g y)) | E 2 82
מודל הסביבות GE E 1 L 2 L 1 f: y: L 3 p: f p: x b: (+ x 3) b: (define y … p: g b: (…) E 2 g: L 4 p: y b: (f (g y)) (L 4 3) | E 1 83
- Infinite diversity in infinite combinations
- Differentiate byte stream and character stream
- Sandwich quotes examples
- Cobit 2019 foundation exam questions
- Types of jet streams
- Characteristics of wetlands
- Cost streams
- Streams aq: waiting for messages in the queue
- Karst topography diagram
- Travelers among mountains and streams
- Sand dune migration
- Streams in c++
- Data nugget streams as sensors answers
- 3 types of fire streams
- Illustrate the proper handling of fire streams
- Oracle streams
- Basic concepts in mining data streams
- A framework for clustering evolving data streams
- Antony searle
- Concept of streams
- Types of bodys of water
- Lisp lazy evaluation
- Oa air washer
- Predefined streams in java
- Gradient definition earth science
- Most streams carry the largest part of their load
- Once there were brook trout in the streams in the mountains
- Poem on growing older
- Disappearing streams karst topography
- Perforce streams tutorial
- Finding frequent items in data streams
- Business model canvas lego
- Tim craddock
- Business model canvas key partners
- Perforce introduction
- Perforce virtual streams
- Discretized stream
- Cba streams
- Here i bow down here i bow down
- Icwpcsd
- Hilbert's paradox of the grand hotel
- Summation notation
- Infinite campus lyons township
- Reaching for the infinite heart bonnie tyler
- Finite set example
- Finite series
- Infinite phrase
- Infinite gp
- Formula for infinite geometric series
- How to find the sum of an infinite series
- Historicisml
- Generalist practice social work definition
- Koch snowflake java
- Finite vs infinite scheduling
- Electric field of infinite line
- Electric field for infinite line charge
- Dimension of a vector space
- Proper subset
- What is a set?
- Partial sum of an infinite series
- Ramanujan infinite series
- Berea infinite campus
- Sum of an arithmetic sequence
- Arithmetic sum formula
- Oscillating discontinuity
- Geometric sequence equation
- Alternating series estimation theorem
- What is a infinite set
- Arithmetic sequence meaning
- Nostalgia of the infinite
- The goal of waiting line management is to minimize
- Sigma notation infinite
- Sd27j infinite campus
- Temporal logic
- Finite potential well wave function
- Infinite potential barrier
- Limits at infinity definition
- Determine the infinite limit
- Barker road middle school
- Infinite campus pittsford
- Mtsd infinite campus
- Semi-infinite solid
- Tkam chapter 27-31 summary
- The main text of the book
- What is a infinite set
- What is a infinite set