SLIDE 1
61A Lecture 30 Announcements Efficient Sequence Processing - - PowerPoint PPT Presentation
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 2
SLIDE 3
Efficient Sequence Processing
SLIDE 4
Sequence Operations
4
SLIDE 5
Sequence Operations
Map, filter, and reduce express sequence manipulation using compact expressions
4
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Streams
SLIDE 22
Streams are Lazy Scheme Lists
6
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Stream Ranges are Implicit
7
A stream can give on-demand access to each element in order
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
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
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
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
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
Infinite Streams
SLIDE 45
Integer Stream
9
SLIDE 46
Integer Stream
An integer stream is a stream of consecutive integers
9
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
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
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
Stream Processing
(Demo)
SLIDE 51
Recursively Defined Streams
11
SLIDE 52
Recursively Defined Streams
The rest of a constant stream is the constant stream
11
SLIDE 53
Recursively Defined Streams
The rest of a constant stream is the constant stream
(define ones (cons-stream 1 ones))
11
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
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
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
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
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
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
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
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
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
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
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
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
Example: Repeats
12
SLIDE 67
Example: Repeats
12
(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a))))
SLIDE 68
Example: Repeats
12
(define a (cons-stream 1 (cons-stream 2 (cons-stream 3 a)))) What's (prefix a 8)? ( __ __ __ __ __ __ __ __ )
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Higher-Order Stream Functions
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
(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
(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
(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
A Stream of Primes
15
SLIDE 91
A Stream of Primes
15
For any prime k, any larger prime must not be divisible by k.
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
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
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
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
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
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
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
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
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
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
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