Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.

Similar presentations


Presentation on theme: "Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16."— Presentation transcript:

1 Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16

2 Finite Streams (define (stream-enumerate-interval low high)
(if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (define s (stream-enumerate-interval )) מבוא מורחב - שיעור 16

3 Infinite streams Since streams are delayed, we are not limited to finite streams!! Some objects are more naturally infinite מבוא מורחב - שיעור 16

4 The integers (define (integers-from n)
(cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1)) מבוא מורחב - שיעור 16

5 Integers not divisible by 7
(define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (stream-filter (lambda (x) (not (divisible? x 7))) integers)) (stream-ref no-sevens 100)  117 מבוא מורחב - שיעור 16

6 Fibonacci sequence (define (fib-seq-from a b)
(cons-stream a (fib-seq-from b (+ a b)))) (define fib-seq (fib-seq-from 0 1)) מבוא מורחב - שיעור 16

7 Fibonacci sequence (define (fib-seq-from a b)
(cons-stream a (fib-seq-from b (+ a b)))) (define fib-seq (fib-seq-from 0 1)) fib-seq (0 . #<struct:promise>[fib-seq-from 1 1]) (stream-ref fib-seq 3) (stream-ref (stream-cdr fib-seq) 2) (stream-ref (fib-seq-from 1 1) 2) (stream-ref (cons 1 (delay (fib-seq-from 1 2))) 2) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 1 2)) 1) (stream-ref (fib-seq-from 1 2) 1) (stream-ref (cons 1 (delay (fib-seq from 2 3))) 1) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 2 3)))) 0) (stream-ref (fib-seq-from 2 3) 0) (stream-ref (cons 2 (delay (fib-seq-from 3 5))) 0) 2 מבוא מורחב - שיעור 16

8 Finding all the primes using the sieve of Eratosthenes
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 60 69 68 67 66 65 64 63 62 61 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 XX X 3 XX 7 XX X 2 XX 5 מבוא מורחב - שיעור 16

9 The sieve of Eratosthenes using streams
(define (sieve str) (cons-stream (stream-car str) (sieve (stream-filter (lambda (x) (not (divisible? x (stream-car str)))) (stream-cdr str))))) (define primes (sieve (stream-cdr integers))) מבוא מורחב - שיעור 16

10 The integers, again (implicit version)
We saw the definition: (define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1)) An alternative definition: (define integers (cons-stream 1 (stream-map (lambda (x) (+ x 1)) integers)) (integers) 1 2 ..... input to stream-map integers 1 2 3 ..... result: element + 1 מבוא מורחב - שיעור 16

11 The integers – another way
(define ones (cons-stream 1 ones)) (define integers (cons-stream 1 (add-streams ones integers)) (define (add-streams s1 s2) (stream-map + s1 s2)) A direct implementation of add-streams: (define (add-streams s1 s2) (cons-stream (+ (stream-car s1) (stream-car s2)) (add-streams (stream-cdr s1) (stream-cdr s2)))) מבוא מורחב - שיעור 16

12 Implicit definition of the Fibonacci numbers
(define fibs (cons-stream 0 (cons-stream 1 (add-streams (stream-cdr fibs) fibs)))) 1 1 2 3 fibs (stream-cdr fibs) 1 1 1 2 3 מבוא מורחב - שיעור 16

13 More implicit definitions
(define (scale-stream str factor) (stream-map (lambda (x) (* x factor)) str) (define double (cons-stream 1 (scale-stream double 2))) 1 (scale-factor double 2) double 2 4 8 מבוא מורחב - שיעור 16

14 The stream of primes – another way
(define primes (cons-stream 2 (stream-filter prime? (integers-from 3)))) (define (prime? n) (define (iter ps) (cond ((> (square (stream-car ps)) n) #t) ((divisible? n (stream-car ps)) #f) (else (iter (stream-cdr ps))))) (iter primes) A recursive definition. Works because the primes needed to check primality of the next number are always ready. מבוא מורחב - שיעור 16

15 Formulating iterations as stream processes
(define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (sqrt-improve guess)))) (sqrt-iter 1.0)) מבוא מורחב - שיעור 16

16 Formulating iterations as stream processes (cont’d)
(define (sqrt-stream x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-improve guess x) (average guess (/ x guess))) (define guesses (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) guesses))) ________________________________________________ ________________________________________________) (stream-car (stream-filter (lambda(x) (good-enough? x)) guesses))) מבוא מורחב - שיעור 16

17 Formulating iterations as stream processes (Cont.)
4 = 1 3 5 7 - + (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) (pi-summands 1) (1 (stream-map - (pi-summands 3))) (1 (stream-map - (1/3 (stream-map - (pi-summands 5)))) (1 -1/3 (stream-map - (stream-map - (pi-summands 5)))) (1 -1/3 +1/5 (stream-map - (stream-map - (stream-map - (pi-summands 7))))) מבוא מורחב - שיעור 16

18 Replacing state variables with streams (Cont.)
4 = 1 3 5 7 - + (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) (define pi-stream (scale-stream (partial-sums (pi-summands 1)) 4)) (display-stream pi-stream) 4. 2.6666 3.4666 2.8952 3.3396 2.9760 S = S0,S0+S1,S0+S1+S2,… converges very slowly! מבוא מורחב - שיעור 16

19 Speeding up convergence
(define (euler-transform s) (let ((s0 (stream-ref s 0)) (s1 (stream-ref s 1)) (s2 (stream-ref s 2))) (cons-stream (- s2 (/ (square (- s2 s1)) (+ s0 (* -2 s1) s2))) (euler-transform (stream-cdr s))))) Sn+1 (Sn+1 - Sn)2 Sn-1 - 2Sn + Sn+1 - S’n = מבוא מורחב - שיעור 16

20 Speeding Up Convergence (Cont.)
(display-stream (euler-transform pi-stream)) 3.1666 3.1333 3.1452 3.1396 3.1427 3.1408 מבוא מורחב - שיעור 16

21 Speeding up convergence even further
(define (make-tableau transform s) (cons-stream s (make-tableau transform (transform s)))) produces a stream of streams: s, (transform s), (transform (transform s)) . . . S00 S01 S02 S03 … S10 S11 S12 … S20 S21 … S30 … מבוא מורחב - שיעור 16

22 Speeding up convergence even further (Cont.)
(define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s))) S00 S01 S02 S S10 S11 S S20 S21 ,. . . . . . (display-stream (accelerated-sequence euler-transform pi-stream)) 4. 3.1666… 3.1421… 3.1415… מבוא מורחב - שיעור 16

23 Infinite streams of pairs
Want to produce the stream of pairs of all integers (i,j) with i  j and bind it to int-pairs. Abstraction: Let’s solve an even more general problem. Given two streams S=(S1, S ) T=(T1, T ) Consider the infinite rectangular array (S1,T1) (S1,T2) (S1,T3) (S2,T1) (S2,T2) (S2,T3) (S3,T1) (S3,T2) (S3,T3) מבוא מורחב - שיעור 16

24 Infinite streams of pairs
(S1,T1) (S1,T2) (S1,T3) (S2,T1) (S2,T2) (S2,T3) (S3,T1) (S3,T2) (S3,T3) Wish to produce all pairs that lie on or above the diagonal: j (S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . i If S and T are both the integers then this would be int-pairs מבוא מורחב - שיעור 16

25 Infinite streams of pairs
(S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . 1 2 3 Write a function pairs such that (pairs s t) would be the desired stream. Wishful thinking: Break the desired stream into three pieces מבוא מורחב - שיעור 16

26 Infinite streams of pairs
(S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . 1 2 3 Produce by (list (stream-car s) (stream-car t)) 1 Produce by (pairs (stream-cdr s) (stream-cdr t)) 3 Produce by (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) 2 מבוא מורחב - שיעור 16

27 Infinite streams of pairs
(define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (combine-in-some-way (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) 1 How do we combine? 2 3 (define (stream-append s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (stream-append (stream-cdr s1) s2)))) מבוא מורחב - שיעור 16

28 Infinite streams of pairs (cont’d)
(define int-pairs (pairs integers integers)) (display-stream int-pairs) (1 1) (1 2) (1 3) (1 4) … will never produce a pair with first component different from 1 !! stream-append does not work when the first stream in infinite We want to be able to produce any pair by applying stream-cdr enough times. מבוא מורחב - שיעור 16

29 Infinite streams of pairs (cont’d)
(define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (interleave (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) מבוא מורחב - שיעור 16

30 Infinite streams of pairs (cont’d)
(define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (interleave (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define int-pairs (pairs integers integers)) (display-stream int-pairs) (1 1) (1 2) (2 2) (1 3) (2 3) (1 4) (3 3) (1 5)… מבוא מורחב - שיעור 16

31 Isn’t there a simpler solution?
(S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . 1 2 Produce by (stream-map (lambda (x) (list (stream-car s) x)) t) 1 Produce by (pairs (stream-cdr s) (stream-cdr t)) 2 (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t))))) מבוא מורחב - שיעור 16

32 Would this work? Infinite loop!!! pairs calls interleave with pairs as a parameter, which causes pairs to be evaluated (S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . 1 2 (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t))))) (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) Because there is no pre-computed “car”, interleave is called again…which calls pairs and so on. מבוא מורחב - שיעור 16

33 The generalized stream-map (multiple streams)
(define (stream-map proc . argstreams) (if (or (null? argstreams) (stream-null? (car argstreams))) the-empty-stream (cons-stream (apply proc (map stream-car argstreams)) (apply stream-map (cons proc (map stream-cdr argstreams)))))) Does it work for finite/infinite streams? What if some are finite and others infinite? What about finite streams with different lengths? מבוא מורחב - שיעור 16


Download ppt "Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16."

Similar presentations


Ads by Google