61a lecture 30 announcements efficient sequence
play

61A Lecture 30 Announcements Efficient Sequence Processing - PowerPoint PPT Presentation

61A Lecture 30 Announcements Efficient Sequence Processing Sequence Operations 4 Sequence Operations Map, filter, and reduce express sequence manipulation using compact expressions 4 Sequence Operations Map, filter, and reduce express


  1. 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)) Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR 6

  2. 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)) Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR 6

  3. 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)) Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (car (cons 1 (/ 1 0))) -> ERROR (car (cons-stream 1 (/ 1 0))) -> 1 (cdr (cons 1 (/ 1 0))) -> ERROR 6

  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)) Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (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 6

  5. 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)) Errors only occur when expressions are evaluated: (cons 1 (/ 1 0)) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (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

  6. Stream Ranges are Implicit A stream can give on-demand access to each element in order 7

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

  8. 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 )))) ( define lots ( range-stream 1 10000000000000000000 )) 7

  9. 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 )))) ( define lots ( range-stream 1 10000000000000000000 )) scm> (car lots) 1 7

  10. 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 )))) ( define lots ( range-stream 1 10000000000000000000 )) scm> (car lots) 1 scm> (car ( cdr-stream lots)) 2 7

  11. 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 )))) ( 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

  12. Infinite Streams

  13. Integer Stream 9

  14. Integer Stream An integer stream is a stream of consecutive integers 9

  15. 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 9

  16. 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 )))) 9

  17. 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 )))) (Demo) 9

  18. Stream Processing (Demo)

  19. Recursively Defined Streams 11

  20. Recursively Defined Streams The rest of a constant stream is the constant stream 11

  21. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 11

  22. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... 11

  23. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... 11

  24. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr 11

  25. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) 11

  26. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) 11

  27. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) 11

  28. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) ( define ints ( cons-stream 1 ( add-streams ones ints))) 11

  29. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) ( define ints ( cons-stream 1 ( add-streams ones ints))) 1 11

  30. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) + ( define ints ( cons-stream 1 ( add-streams ones ints))) 1 11

  31. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) + ( define ints ( cons-stream 1 ( add-streams ones ints))) 1 2 11

  32. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) + + ( define ints ( cons-stream 1 ( add-streams ones ints))) 1 2 11

  33. Recursively Defined Streams The rest of a constant stream is the constant stream ( define ones ( cons-stream 1 ones)) 1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr ( define ( add-streams s t) ( cons-stream (+ (car s) (car t)) ( add-streams ( cdr-stream s) ( cdr-stream t)))) + + ( define ints ( cons-stream 1 ( add-streams ones ints))) 2 3 4 5 6 7 ... 1 2 11

  34. Example: Repeats 12

  35. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) 12

  36. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 12

  37. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 12

  38. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  39. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  40. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  41. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  42. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  43. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  44. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  45. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  46. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  47. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  48. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  49. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 2 2 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  50. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 2 2 3 3 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  51. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 2 2 3 3 3 3 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  52. Example: Repeats ( define a ( cons-stream 1 ( cons-stream 2 ( cons-stream 3 a)))) ( define ( f s) ( cons-stream (car s) ( cons-stream (car s) ( f ( cdr-stream s))))) ( define ( g s) ( cons-stream (car s) ( f ( g ( cdr-stream s))))) 1 2 3 1 2 3 1 2 What's (prefix a 8)? ( __ __ __ __ __ __ __ __ ) 1 1 2 2 3 3 1 1 What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ ) 1 2 2 3 3 3 3 1 What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) 12

  53. Higher-Order Stream Functions

  54. Higher-Order Functions on Streams Implementations are identical, but change cons to cons-stream and change cdr to cdr-stream (Demo) 14

  55. Higher-Order Functions on Streams ( define ( map f s) ( if (null? s) Implementations are identical, nil but change cons to cons-stream (cons ( f (car s)) and change cdr to cdr-stream (map f (cdr s))))) ( define ( filter f s) ( if (null? s) nil ( if ( f (car s)) (cons (car s) ( filter f (cdr s))) ( filter f (cdr s))))) ( define ( reduce f s start) ( if (null? s) start ( reduce f (Demo) (cdr s) ( f start (car s))))) 14

  56. Higher-Order Functions on Streams ( define ( map f s) ( if (null? s) Implementations are identical, nil but change cons to cons-stream (cons ( f (car s)) and change cdr to cdr-stream (map f (cdr s))))) ( define ( filter f s) ( if (null? s) nil ( if ( f (car s)) (cons (car s) ( filter f (cdr s))) ( filter f (cdr s))))) ( define ( reduce f s start) ( if (null? s) start ( reduce f (Demo) (cdr s) ( f start (car s))))) 14

  57. Higher-Order Functions on Streams ( define ( map-stream f s) ( if (null? s) Implementations are identical, nil but change cons to cons-stream ( cons-stream ( f (car s)) and change cdr to cdr-stream ( map-stream f ( cdr-stream s))))) ( define ( filter-stream f s) ( if (null? s) nil ( if ( f (car s)) ( cons-stream (car s) ( filter-stream f ( cdr-stream s))) ( filter-stream f ( cdr-stream s))))) ( define ( reduce-stream f s start) ( if (null? s) start ( reduce-stream f (Demo) ( cdr-stream s) ( f start (car s))))) 14

  58. A Stream of Primes 15

  59. A Stream of Primes For any prime k, any larger prime must not be divisible by k. 15

  60. 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: 15

  61. 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 15

  62. 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 15

  63. 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 15

  64. 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 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 15

  65. 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 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 15

  66. 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 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 15

  67. 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 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 15

  68. 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 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 15

Recommend


More recommend