announcements 61a lecture 31
play

Announcements 61A Lecture 31 Sequence Operations Map, filter, and - PDF document

Announcements 61A Lecture 31 Sequence Operations Map, filter, and reduce express sequence manipulation using compact expressions Example: Sum all primes in an interval from a (inclusive) to b (exclusive) def sum_primes (a, b): def sum_primes (a,


  1. Announcements 61A Lecture 31 Sequence Operations Map, filter, and reduce express sequence manipulation using compact expressions Example: Sum all primes in an interval from a (inclusive) to b (exclusive) def sum_primes (a, b): def sum_primes (a, b): total = 0 return sum(filter(is_prime, range(a, b))) Efficient Sequence Processing x = a sum_primes (1, 6) while x < b: if is_prime(x): sum filter range iterator total = total + x source: source: next: 4 5 1 2 3 x = x + 1 return total total: 5 10 2 0 end: 6 f: is_prime Space: Θ (1) Θ (1) (Demo) 4 Streams are Lazy Scheme Lists A stream is a list, but the rest of the list is computed only when needed: (car (cons 1 2)) -> 1 (car (cons-stream 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cdr-stream (cons-stream 1 2)) -> 2 (cons 1 (cons 2 nil)) (cons-stream 1 (cons-stream 2 nil)) Streams Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[promise (not forced)]) (car (cons 1 (/ 1 0))) -> ERROR (car (cons-stream 1 (/ 1 0))) -> 1 (cdr (cons 1 (/ 1 0))) -> ERROR (cdr-stream (cons-stream 1 (/ 1 0))) -> ERROR (Demo) 6 Stream Ranges are Implicit A stream can give on-demand access to each element in order ( define ( range-stream a b) ( if (>= a b) nil ( cons-stream a ( range-stream ( + a 1) b )))) Infinite Streams ( define lots ( range-stream 1 10000000000000000000 )) scm> (car lots) 1 scm> (car ( cdr-stream lots)) 2 scm> (car ( cdr-stream ( cdr-stream lots))) 3 7

  2. Integer Stream An integer stream is a stream of consecutive integers The rest of the stream is not yet computed when the stream is created ( define ( int-stream start) ( cons-stream start ( int-stream (+ start 1 )))) Stream Processing (Demo) (Demo) 9 Recursively Defined Streams Example: Repeats The rest of a constant stream is the constant stream ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) 1 1 1 1 1 1 ... ( define ones ( cons-stream 1 ones)) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) Combine two streams by separating each into car and cdr ( f ( cdr-stream s))))) ( define ( add-streams s t) ( define ( g s) ( cons-stream (car s) ( cons-stream (+ (car s) (car t)) ( f ( g ( cdr-stream s))))) ( add-streams ( cdr-stream s) ( cdr-stream t)))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) + + 1 1 2 2 3 3 1 1 ( define ints ( cons-stream 1 ( add-streams ones ints))) 2 3 4 5 6 7 ... 1 2 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 2 2 3 3 3 3 1 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 11 12 Higher-Order Functions on Streams ( define ( map-stream f s) ( define ( map f s) ( define ( map f s) ( if (null? s) ( if (null? s) ( if (null? s) Implementations are identical, but change cons to cons-stream nil nil nil and change cdr to cdr-stream ( cons-stream ( f (car s)) (cons ( f (car s)) (cons ( f (car s)) ( map-stream f (map f (map f ( cdr-stream s))))) (cdr s))))) (cdr s))))) ( define ( filter-stream f s) ( define ( filter f s) ( define ( filter f s) Higher-Order Stream Functions ( if (null? s) ( if (null? s) ( if (null? s) nil nil nil ( if ( f (car s)) ( if ( f (car s)) ( if ( f (car s)) (cons (car s) (cons (car s) ( cons-stream (car s) ( filter f (cdr s))) ( filter f (cdr s))) ( filter-stream f ( cdr-stream s))) ( filter f (cdr s))))) ( filter-stream f ( cdr-stream s))))) ( filter f (cdr s))))) ( define ( reduce-stream f s start) ( define ( reduce f s start) ( define ( reduce f s start) ( if (null? s) ( if (null? s) ( if (null? s) start start start ( reduce-stream f ( reduce f ( reduce f (cdr s) ( cdr-stream s) (cdr s) ( f start (car s))))) ( f start (car s))))) ( f start (car s))))) 14 A Stream of Primes For any prime k, any larger prime must not be divisible by k. The stream of integers not divisible by any k <= n is: • The stream of integers not divisible by any k < n • Filtered to remove any element divisible by n This recurrence is called the Sieve of Eratosthenes Promises 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 (Demo) 15

  3. Implementing Streams with Delay and Force A promise is an expression, along with an environment in which to evaluate it Delaying an expression creates a promise to evaluate it later in the current environment Forcing a promise returns its value in the environment in which it was defined scm> (define promise (let ((x 2)) (delay (+ x 1)) )) (define promise (let ((x 2)) (lambda () (+ x 1)) )) scm> (define x 5) (define-macro (delay expr) `(lambda () ,expr)) scm> (force promise) (define (force promise) (promise)) 3 A stream is a list, but the rest of the list is computed only when forced : scm> (define ones (cons-stream 1 ones)) (1 . #[promise (not forced)]) (define-macro (cons-stream a b) `(cons ,a (delay ,b))) (1 . (lambda () ones)) (define (cdr-stream s) (force (cdr s))) 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend