Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18 Infinite Streams 3.5.2 and 3.5.3.

Similar presentations


Presentation on theme: "Lecture 18 Infinite Streams 3.5.2 and 3.5.3."— Presentation transcript:

1 Lecture 18 Infinite Streams 3.5.2 and 3.5.3

2 Review of Material From the Last Lecture

3 Delay and Force (delay <exp>) ==> a promise to evaluate exp
(force <delayed object>) ==> evaluate the delayed object and return the result (define x (delay (+ 1 1))) x  #<promise> (force x)  2 (delay <exp>) is a special form. force is not a special form.

4 What are these mysterious delay and force ?
delay is a special form such that (delay <exp>) is equivalent to (lambda () <exp>) force is a procedure that calls a procedure produced by delay: (define (force delayed-object) (delayed-object))

5 The Streams Interface Constructors: the-empty-stream (cons-stream x y)
Selectors: (stream-car (cons-stream x y)) = x (stream-cdr (cons-stream x y)) = y Method: (stream-null? x) cons-stream treats its second argument as a delayed object.

6 Streams via delay and force
(cons-stream <a> <b>) is a special form equivalent to (cons <a> (delay <b>)) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream)))

7 Enumerating with 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 1 3)) (stream-enumerate-interval 1 3) (cons-stream 1 (stream-enumerate-interval 2 3)) (cons 1 (delay (stream-enumerate-interval 2 3))) (cons 1 (lambda () (stream-enumerate-interval 2 3)))

8 stream-ref, stream-map
(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))))

9 Stream-filter (define (stream-filter pred stream)
(cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))) (else (stream-filter pred (stream-cdr stream)))))

10 Applicative vs. Normal order evaluation.
(car (cdr (filter prime? (enu-int )))) (stream-car (stream-cdr (stream-filter prime?(str-enu-int )))) Both return the second prime larger than 10 (which is 13) With lists it takes about operations With streams about three.

11 Forcing a delayed object many times
Suppose we have the following scenario: (define x (delay (very-hard-function a))) (force x) We need to call the hard function twice. Scheme will automatically detect that this is the second time we try to evaluate the funtion and use the value we have evaluated before. Scheme makes it look like: (define x (delay (very-hard-function a))) (define b (force x)) b

12 How is it done? We redefine delay as follows
(delay <exp>) translates to (memo-proc (lambda () <exp>)) (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))))

13 Infinite streams – Defining all integers.
(define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1))

14 Defining all 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

15 Infinite streams – Fibonachi 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 Today: Exploiting the Streams Paradigm

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

18 (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

19 More implicit definitions of streams
(define (add-streams s1 s2) (stream-map + s1 s2)) (define integers (cons-stream 1 (add-streams ones integers)))

20 More implicit definitions of streams
(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

21 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)))

22 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))

23 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))

24 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)

25 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 . . .

26 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)))))

27 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,…

28 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 =

29 Speeding Up Convergence (Cont.)
(display-stream (euler-transform pi-stream)) 3.1666 3.1333 3.1452 3.1396 3.1427 2.1408

30 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)) . . . S00 S01 S02 S S10 S11 S S20 S21 ,. . . . . .

31 Speading 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

32 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 ?

33 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)

34 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

35 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.

36 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

37 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))))

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

39 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)))))


Download ppt "Lecture 18 Infinite Streams 3.5.2 and 3.5.3."

Similar presentations


Ads by Google