Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18.

Similar presentations


Presentation on theme: "Lecture 18."— Presentation transcript:

1 Lecture 18

2 Infinite streams -- more examples
(define (fibgen a b) (cons-stream a (fibgen b (+ a b)))) (define fibs (fibgen 0 1))

3 Finding all the primes XX 5 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

4 Remember our sieve? (sieve (2 3 4 . . . ))
(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))) (sieve ( )) (2 (sieve (stream-filter /2 ( )))) (2 (sieve (3 (stream-filter /2 ( ))))) (2 3 (sieve (stream-filter /3 (stream-filter /2 ( )))))

5 Pitfalls (stream-filter odd? (stream-filter even? integers))
(stream-null? (stream-filter odd? (stream-filter even? integers)))

6 Defining streams implicitely
(define ones (cons-stream 1 ones))

7 (define ones (cons-stream 1 ones)) | GE
(define ones (cons 1 (memo-proc (lambda () ones)))) | GE (stream-cdr ones) | GE ones: GE proc: #t p: b: ones 1 already-run: #f result: #f

8 More implicit definitions of streams
(define integers (cons-stream 1 (add-streams ones integers))) (define fibs (cons-stream 0 (cons-stream 1 (add-streams (stream-cdr fibs) fibs)))) fibs (stream-cdr fibs) 1 1 2 3 1 1 1 2 3

9 More implicit definitions of streams
(define (scale-stream stream factor) (stream-map (lambda (x) (* x factor)) stream)) (define double (cons-stream 1 (scale-stream double 2)))

10 More implicit definitions of streams
(define primes (cons-stream 2 (stream-filter prime? (integers-starting-from 3)))) (define (prime? n) (define (iter ps) (cond ((> (square (stream-car ps)) n) true) ((divisible? n (stream-car ps)) false) (else (iter (stream-cdr ps))))) (iter primes))

11 Replacing state variables with streams
(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))

12 Replacing state variables with streams
(define (sqrt-improve guess x) (average guess (/ x guess))) (define (sqrt-stream x) (define guesses (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) guesses))) guesses)

13 Replacing state variables with streams
(define (display-stream s) (stream-for-each display-line s)) (define (display-line x) (newline) (display x)) (display-stream (sqrt-stream 2)) 1. 1.5 . . .

14 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))))) (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)))))

15 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

16 Speading 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 = (display-stream (euler-transform pi-stream)) 3.1666 3.1333 3.1452 3.1396 3.1427 2.1408

17 Speading up convergence even further
(define (make-tableau transform s) (cons-stream s (make-tableau transform (transform s)))) produces stream of streams: s, (transform s), (transform (transform s)) . . . (define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s)))

18 Speading up convergence even further (Cont.)
(display-stream (accelerated-sequence euler-transform pi-stream)) 4. 3.1666 3.1421 3.1415

19 Infinite streams of pairs
Want to produce the stream of pairs of all integers (i,j) with i  j such that i + j is prime. (1,1) (1,2) (1,4) (2,3) ……. Reduce the problem: (stream-filter (lambda (pair) (prime? (+ (car pair) (cadr pair)))) int-pairs) What should we bind int-pairs to ?

20 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: Lets 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)

21 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: (S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . If S and T are both the integers then this would be int-pairs

22 Infinite streams of pairs
Wishful thinking: Break the desired stream into three pieces 1 2 3 (S1,T1) (S1,T2) (S1,T3) (S2,T2) (S2,T3) (S3,T3) . . . Write a function pairs such that (pairs S T) would be the desired stream.

23 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

24 Infinite streams of pairs
(define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (stream-append (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define (stream-append s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (stream-append (stream-cdr s1) s2)))) (define int-pairs (pairs integers integers))

25 Infinite streams of pairs
This implementation is not good !! (stream-car (stream-cdr (stream-cdr int-pairs))))))) .. will never produce a pair with first component different from 1 !! Want to be able to produce any pair by applying stream-cdr enough times.

26 Infinite streams of pairs
(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))))) (define int-pairs (pairs integers integers))


Download ppt "Lecture 18."

Similar presentations


Ads by Google