Presentation is loading. Please wait.

Presentation is loading. Please wait.

Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal.

Similar presentations


Presentation on theme: "Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal."— Presentation transcript:

1 Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal laser light music

2 Functions that work on lists (map square ‘(1 2 3 4 5)) ==> (1 4 9 16 25) (filter odd ‘(1 2 3 4 5)) ==> (1 3 5) (accumulate + 0 ‘(1 2 3 4 5)) ==> 15 Streams similar to lists, but can be infinitely long (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... (0 1 2 3 4 5 6 7 8 9 10 11 12 13...

3 Sum squares of primes between 2 and n (define (sum-prime-squares ) (method ((n )) (bind-methods ((f ((k ) (sum )) (cond ((> k n) sum) ((prime? k) (f (inc k) (+ sum (square k)))) (else: (f (inc k) sum))))) (f 2 0)))) (sum-prime-squares 5) ==> 38

4 1. enumerate the numbers 1...n 2. filter out the nonprimes from that list 3. map square on each member of the list 4. accumulate the sum starting from 0 enumeratefiltermapaccumulatensum

5 Operations on streams (pair-stream obj stream) (empty? stream) (head stream) (tail stream) empty-stream Contract (head (pair-stream obj stream)) ==> obj (tail (pair-stream obj stream)) ==> stream (empty? empty-stream) ==> #t (empty? (pair-stream obj stream)) ==> #f

6 ;;(interval 2 n) produces stream (2... n) (define (interval ) (method ((low ) (high )) (if (> low high) empty-stream (pair-stream low (interval (inc low) high))))) (define (sum-prime-squares ) (method ((n )) (accumulate + 0 (map square (filter prime? (interval 2 n))))))

7 (add-method filter (method ((test ) (stream )) (cond ((empty? stream) empty-stream) ((test (head stream)) (pair-stream (head stream) (filter test (tail stream)))) (else: (filter test (tail stream))))))) (add-method map (method ((f ) (stream )) (if (empty? stream) empty-stream (pair-stream (f (head stream)) (map f (tail stream)))))) (add-method accumulate (method ((op ) (init ) (stream )) (if (empty? stream) init (op (head stream) (accumulate op init (tail stream)))))))

8 Why bother? 1. It's a very powerful metaphor. The famous and popular UNIX system gets major mileage out of this view (pipes) 2. Once you've got a decent library of stream functions, you can throw together fancy programs quite fast. That's UNIX's major win. 3. Computations over infinite data structures: (1 1 1 1 1... (0 1 2 3 4...

9 "What is the second prime between 10,000 and 93,000,000?" (head (tail (filter prime? (interval 10000 93000000)))) Moral: Be lazy!

10 Difference between streams and lists Lists: eager evaluation Streams: lazy evaluation With a stream, the tail is not evaluated when you make the stream, but only when you use it Same contract, but evaluation happens at a different time (pair-stream h t) does not evaluate t (head (pair-stream h t)) ==> h ( t not evaluated yet) (tail (pair-stream h t)) ==> t (now t is evaluated)

11 Delay and force (delay x) ==> a promise to evaluate x when asked to (force y) ==> collects on the promise

12 (define (y ) (delay (/ 1 0))) ==> y (force y) ==> Error: division by zero (define yy (method () (/ 1 0))) ==> yy (yy) ==> Error: division by zero

13 (define (delay ) (macro (x) (list method () x))) (delay expr) expands to (method () expr) (define (force ) (method ((promise )) (promise)))

14 Definition of streams (define-class ( )) (define-class ( ) (head-stream ) (tail-stream )) (define (pair-stream ) (macro (o s) (list make head-stream: o tail-stream: (list delay s))))

15 (pair-stream h t) expands to (make head-stream: h tail-stream: (delay t)) (add-method head (method ((s )) (head-stream s))) (add-method tail (method ((s )) (force (tail-stream s))))

16 (pair-stream (+ 1 1) (+ 2 2)) ==> (make head-stream: (+ 1 1) tail-stream: (delay (+ 2 2))) ==> (make head-stream: 2 tail-stream: (method () (+ 2 2))) ==> (2. {method () (+ 2 2)})

17 (head (tail (filter prime? (interval 10000 93000000)))) (interval 10000 93000000) evaluates to a (10000. {method () (interval 10001 93000000)})

18 (define (ones ) (pair-stream 1 ones)) ones = (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... (define (naturals ) (bind-methods ((start-from ((x )) (pair-stream x (start-from (inc x))))) (start-from 0))) naturals = (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14...


Download ppt "Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal."

Similar presentations


Ads by Google