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

61a lecture 30 announcements efficient sequence
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

61A Lecture 30

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Efficient Sequence Processing

slide-4
SLIDE 4

Sequence Operations

4

slide-5
SLIDE 5

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

slide-6
SLIDE 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

slide-7
SLIDE 7

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total

slide-8
SLIDE 8

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total

Space:

Θ(1)

slide-9
SLIDE 9

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

slide-10
SLIDE 10

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

sum_primes(1, 6)

slide-11
SLIDE 11

range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1

sum_primes(1, 6)

slide-12
SLIDE 12

range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime

sum_primes(1, 6)

slide-13
SLIDE 13

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime

sum_primes(1, 6)

slide-14
SLIDE 14

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime 2

sum_primes(1, 6)

slide-15
SLIDE 15

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime 2 3 2

sum_primes(1, 6)

slide-16
SLIDE 16

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime 2 3 4 5

sum_primes(1, 6)

slide-17
SLIDE 17

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime 2 3 4 5 5

sum_primes(1, 6)

slide-18
SLIDE 18

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1)

1 filter source: f: is_prime 2 3 4 5 10

sum_primes(1, 6)

slide-19
SLIDE 19

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1) Θ(1)

1 filter source: f: is_prime 2 3 4 5 10

sum_primes(1, 6)

slide-20
SLIDE 20

sum source: total: range iterator next: end: 6

Sequence Operations

Map, filter, and reduce express sequence manipulation using compact expressions

4

Example: Sum all primes in an interval from a (inclusive) to b (exclusive)

def sum_primes(a, b): total = 0 x = a while x < b: if is_prime(x): total = total + x x = x + 1 return total def sum_primes(a, b): return sum(filter(is_prime, range(a, b)))

Space:

Θ(1) Θ(1)

(Demo) 1 filter source: f: is_prime 2 3 4 5 10

sum_primes(1, 6)

slide-21
SLIDE 21

Streams

slide-22
SLIDE 22

Streams are Lazy Scheme Lists

6

slide-23
SLIDE 23

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

slide-24
SLIDE 24

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1

slide-25
SLIDE 25

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2

slide-26
SLIDE 26

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil))

slide-27
SLIDE 27

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1

slide-28
SLIDE 28

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2

slide-29
SLIDE 29

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil))

slide-30
SLIDE 30

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) Errors only occur when expressions are evaluated:

slide-31
SLIDE 31

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR Errors only occur when expressions are evaluated:

slide-32
SLIDE 32

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR Errors only occur when expressions are evaluated:

slide-33
SLIDE 33

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR Errors only occur when expressions are evaluated:

slide-34
SLIDE 34

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) Errors only occur when expressions are evaluated:

slide-35
SLIDE 35

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (car (cons-stream 1 (/ 1 0))) -> 1 Errors only occur when expressions are evaluated:

slide-36
SLIDE 36

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (car (cons-stream 1 (/ 1 0))) -> 1 (cdr-stream (cons-stream 1 (/ 1 0))) -> ERROR Errors only occur when expressions are evaluated:

slide-37
SLIDE 37

Streams are Lazy Scheme Lists

A stream is a list, but the rest of the list is computed only when needed:

6

(car (cons 1 2)) -> 1 (cdr (cons 1 2)) -> 2 (cons 1 (cons 2 nil)) (Demo) (car (cons-stream 1 2)) -> 1 (cdr-stream (cons-stream 1 2)) -> 2 (cons-stream 1 (cons-stream 2 nil)) (cons 1 (/ 1 0)) -> ERROR (car (cons 1 (/ 1 0))) -> ERROR (cdr (cons 1 (/ 1 0))) -> ERROR (cons-stream 1 (/ 1 0)) -> (1 . #[delayed]) (car (cons-stream 1 (/ 1 0))) -> 1 (cdr-stream (cons-stream 1 (/ 1 0))) -> ERROR Errors only occur when expressions are evaluated:

slide-38
SLIDE 38

Stream Ranges are Implicit

7

A stream can give on-demand access to each element in order

slide-39
SLIDE 39

Stream Ranges are Implicit

7

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

slide-40
SLIDE 40

Stream Ranges are Implicit

7

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

slide-41
SLIDE 41

Stream Ranges are Implicit

7

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

slide-42
SLIDE 42

Stream Ranges are Implicit

7

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

slide-43
SLIDE 43

Stream Ranges are Implicit

7

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

slide-44
SLIDE 44

Infinite Streams

slide-45
SLIDE 45

Integer Stream

9

slide-46
SLIDE 46

Integer Stream

An integer stream is a stream of consecutive integers

9

slide-47
SLIDE 47

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

slide-48
SLIDE 48

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

slide-49
SLIDE 49

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

(Demo)

slide-50
SLIDE 50

Stream Processing

(Demo)

slide-51
SLIDE 51

Recursively Defined Streams

11

slide-52
SLIDE 52

Recursively Defined Streams

The rest of a constant stream is the constant stream

11

slide-53
SLIDE 53

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

slide-54
SLIDE 54

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

1 1 1 1 1 1 ...

slide-55
SLIDE 55

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

1 1 1 1 1 1 ...

slide-56
SLIDE 56

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr

slide-57
SLIDE 57

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

1 1 1 1 1 1 ... Combine two streams by separating each into car and cdr

(define (add-streams s t)

slide-58
SLIDE 58

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-59
SLIDE 59

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-60
SLIDE 60

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-61
SLIDE 61

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-62
SLIDE 62

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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 +

slide-63
SLIDE 63

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-64
SLIDE 64

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-65
SLIDE 65

Recursively Defined Streams

The rest of a constant stream is the constant stream

(define ones (cons-stream 1 ones))

11

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

slide-66
SLIDE 66

Example: Repeats

12

slide-67
SLIDE 67

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a))))

slide-68
SLIDE 68

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

slide-69
SLIDE 69

Example: Repeats

12

(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)? ( __ __ __ __ __ __ __ __ )

slide-70
SLIDE 70

Example: Repeats

12

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

slide-71
SLIDE 71

Example: Repeats

12

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

slide-72
SLIDE 72

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

slide-73
SLIDE 73

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3

slide-74
SLIDE 74

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ ) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-75
SLIDE 75

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-76
SLIDE 76

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-77
SLIDE 77

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-78
SLIDE 78

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-79
SLIDE 79

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ ) (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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-80
SLIDE 80

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ )

1

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-81
SLIDE 81

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ )

1 2 2

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-82
SLIDE 82

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ )

1 2 2 3 3

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-83
SLIDE 83

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ )

1 2 2 3 3 3 3

(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 (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-84
SLIDE 84

Example: Repeats

12

(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix (g a) 8)? ( __ __ __ __ __ __ __ __ )

1 2 2 3 3 3 3

(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

What's (prefix (f a) 8)? ( __ __ __ __ __ __ __ __ )

1 1 2 2 3 3 1 1

What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )

1 2 3 1 2 3 1 2

slide-85
SLIDE 85

Higher-Order Stream Functions

slide-86
SLIDE 86

Higher-Order Functions on Streams

14

Implementations are identical, but change cons to cons-stream and change cdr to cdr-stream (Demo)

slide-87
SLIDE 87

(define (map f s) (if (null? s) nil (cons (f (car s)) (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 (cdr s) (f start (car s)))))

Higher-Order Functions on Streams

14

Implementations are identical, but change cons to cons-stream and change cdr to cdr-stream (Demo)

slide-88
SLIDE 88

(define (map f s) (if (null? s) nil (cons (f (car s)) (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 (cdr s) (f start (car s)))))

Higher-Order Functions on Streams

14

Implementations are identical, but change cons to cons-stream and change cdr to cdr-stream (Demo)

slide-89
SLIDE 89

(define (map-stream f s) (if (null? s) nil (cons-stream (f (car s)) (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 (cdr-stream s) (f start (car s)))))

Higher-Order Functions on Streams

14

Implementations are identical, but change cons to cons-stream and change cdr to cdr-stream (Demo)

slide-90
SLIDE 90

A Stream of Primes

15

slide-91
SLIDE 91

A Stream of Primes

15

For any prime k, any larger prime must not be divisible by k.

slide-92
SLIDE 92

A Stream of Primes

The stream of integers not divisible by any k <= n is:

15

For any prime k, any larger prime must not be divisible by k.

slide-93
SLIDE 93

A Stream of Primes

The stream of integers not divisible by any k <= n is:

  • The stream of integers not divisible by any k < n

15

For any prime k, any larger prime must not be divisible by k.

slide-94
SLIDE 94

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-95
SLIDE 95

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-96
SLIDE 96

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-97
SLIDE 97

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-98
SLIDE 98

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-99
SLIDE 99

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-100
SLIDE 100

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-101
SLIDE 101

A Stream of Primes

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

For any prime k, any larger prime must not be divisible by k.

slide-102
SLIDE 102

A Stream of Primes

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

(Demo) For any prime k, any larger prime must not be divisible by k.