Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>

Similar presentations


Presentation on theme: "מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>"— Presentation transcript:

1 מבוא מורחב - שיעור 15 1 Lecture 15 Streams

2 מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) ((prime? count) (iter (+ count 1) (+ count accum))) (else (iter (+ count 1) accum)))) (iter a 0)) (define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) Second implementation consumes a lot of storage

3 מבוא מורחב - שיעור 15 3 Streams: Motivation (Cont.) (car (cdr (filter prime? (enumerate-interval 10000 1000000)))) Requires a lot of time and space How can we gain the efficiency of iteration, and retain the elegance of sequence-operations? Streams!

4 מבוא מורחב - שיעור 17 4 Remember normal order evaluation? Normal (Lazy) Order Evaluation: go ahead and apply operator with unevaluated argument subexpressions evaluate a subexpression only when value is needed –to print –by primitive procedure (that is, primitive procedures are "strict" in their arguments) Compromise approach: give programmer control between normal and applicative order. Streams: lists with delays

5 מבוא מורחב - שיעור 15 5 Streams interface Constructors: the-empty-stream (cons-stream x y) Predicate: ( stream-null? x) cons-stream treats its second argument as a delayed object. Selectors: (stream-car (cons-stream x y)) = x (stream-cdr (cons-stream x y)) = y

6 מבוא מורחב - שיעור 15 6 Streams via delay and force (cons-stream ) is a special form equivalent to (cons (delay )) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream)))

7 מבוא מורחב - שיעור 15 7 delay and force (delay ) ==> a promise to evaluate exp (force ) ==> evaluate the delayed object and return the result (define x (delay (+ 1 1))) x  # (force x)  2 (delay ) is a special form. force is not a special form.

8 מבוא מורחב - שיעור 15 8 What are these mysterious delay and force ? delay is a special form such that (delay ) is equivalent to (lambda () ) force is a procedure that calls a procedure produced by delay: (define (force delayed-object) (delayed-object))

9 מבוא מורחב - שיעור 15 9 Let us recall lists (define (enumerate-interval low high) (if (> low high) ‘() (cons low (enumerate-interval (+ low 1) high)))) (define int123 (enumerate-interval 1 3)) (enumerate-interval 1 3) (cons 1 (enumerate-interval 2 3)) (cons 1 (cons 2 (enumerate-interval 3 3))) (cons 1 (cons 2 (cons 3 (enumerate-interval 4 3))) (cons 1 (cons 2 (cons 3 ‘())))

10 מבוא מורחב - שיעור 15 10 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)))

11 מבוא מורחב - שיעור 15 11 (define s (stream-enumerate-interval 1 3)) | GE GE p: low high b : (if (> low high) the-empty-stream (cons-stream... ) stream-enumerate-interval: (cons-stream 1 (str-enu-int (+ low 1) high))| E1 (cons 1 (lambda () (str-enu-int (+ low 1) high)) | E1 p: b: (str-enu_int (+ low 1) high) s: 1 low 1 high 3 E1

12 מבוא מורחב - שיעור 15 12 (define s1 (stream-cdr s)) (stream-cdr s) (stream-cdr (cons 1 (lambda () (str-enu-int 2 3)))) (force (cdr (cons 1 (lambda () (str-enu-int 2 3))))) (force (lambda () (str-enu-int 2 3))) ((lambda () (str-enu-int 2 3))) (str-enu-int 2 3) (cons-stream 2 (str-enu-int 3 3)) (cons 2 (delay (str-enu-int 3 3))) (cons 2 (lambda () (str-enu-int 3 3))) Calling stream-cdr

13 מבוא מורחב - שיעור 15 13 (define s1 (stream-cdr s)) | GE (force (cdr s)) | GE GE p: low high b : (if (> low high) the-empty-stream (cons-stream... ) str-enu-int: s: E1 low 1 1 high 3 p: b:(str-enu-int (+ low 1) high) 2 low 2 high 3 s1:

14 מבוא מורחב - שיעור 15 14 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)))) Also a version with multiple stream arguments

15 מבוא מורחב - שיעור 15 15 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)))))

16 מבוא מורחב - שיעור 15 16 Applicative vs. Normal order evaluation. (stream-car (stream-cdr (stream-filter prime?(str-enu-int 10 1000000)))) (car (cdr (filter prime? (enu-int 10 1000000)))) Both return the second prime larger or equal to 10 (which is 13) With lists it takes about 1000000 operations With streams about three.

17 מבוא מורחב - שיעור 15 17 How does it work? - I (stream-car (stream-cdr (stream-filter prime? (str-enu-int 10 1000)))) (stream-car (stream-cdr (stream-filter prime? (cons 10 (delay (str-enu-int 11 1000)))))) (stream-car (stream-cdr (stream-filter prime? (force (lambda () (str-enu-int 11 1000)))))) (stream-car (stream-cdr (stream-filter prime? (cons 11 (delay (str-enu-int 12 1000))))))

18 מבוא מורחב - שיעור 15 18 How does it work? - II (stream-car (stream-filter prime? (stream-cdr (cons 11 (delay (str-enu-int 12 1000)))))) (stream-car (stream-cdr (cons 11 (delay (stream-filter prime? (stream-cdr (cons 11 (delay (str-enu-int 12 1000))))))))) (stream-car (stream-filter prime? (str-enu-int 12 1000)))

19 מבוא מורחב - שיעור 15 19 How does it work? - III (stream-car (stream-filter prime? (cons 12 (delay (str-enu-int 13 1000))))) (stream-car (stream-filter prime? (str-enu-int 13 1000))) (stream-car (stream-filter prime? (cons 13 (delay (str-enu-int 14 1000))))) (stream-car (cons 13 (delay (stream-filter prime? (stream-cdr (cons 13 (delay (str-enu-int 14 1000)))))))) 13

20 Memoization 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 function and use the value we have evaluated before.

21 How is it done? delay is actually defined as follows: (delay ) translates to (memo-proc (lambda () )) (define (memo-proc proc) (let ((already-run? #f) (result #f)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result))))

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

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

24 מבוא מורחב - שיעור 15 24 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

25 מבוא מורחב - שיעור 15 25 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))

26 מבוא מורחב - שיעור 15 26 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. # [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

27 מבוא מורחב - שיעור 15 27 Finding all the primes using the sieve of Eratosthenes

28 מבוא מורחב - שיעור 15 28 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)))

29 מבוא מורחב - שיעור 15 29 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) integers 1 1 2 2 3 input to stream-map result: element + 1.....


Download ppt "מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>"

Similar presentations


Ads by Google