140 likes | 283 Views
CMSC 11500 Introduction to Computer Programming November 18, 2002. Streams: Infinite Data. Roadmap . Recap: Streams Basics Streams Streams and Abstraction Map-stream Filter Natural numbers: Add1-stream, Add-streams Finding Primes Seive of Eratosthenes. Recap: Streams Basics.
E N D
CMSC 11500 Introduction to Computer Programming November 18, 2002 Streams:Infinite Data
Roadmap • Recap: Streams Basics • Streams • Streams and Abstraction • Map-stream • Filter • Natural numbers: • Add1-stream, Add-streams • Finding Primes • Seive of Eratosthenes
Recap: Streams Basics • Modeling infinite data • Delay evaluation until needed • Delay: anything -> promise • Force: promise -> anything • A stream-of-numbers is: • (cons number (delay stream-of-numbers)) • (car stream), (force (cdr stream)) • Template: (define (fn-for-stream stream) • ... (car stream) .... • ... (fn-for-stream (force (cdr stream))))
Map-stream Tr Contract: map-stream: (number->number) stream-of-numbers -> stream-of-numbers Purpose: Apply function to all elements of stream (define (map-stream func stream) (cons (func (car stream)) (delay (map-stream func (force (cdr stream))))) (define (square x) (* x x)) (define (square-stream stream) (map-stream square stream))
Stream-enum-int • Contract: number number -> stream • Produce a stream of numbers between a & b
E Stream-enum-int (define (stream-enum-int a b) (cond ((> a b) the-empty-stream) (else (cons-stream a (stream-enum-int (+ a 1) b))) (car (cdr (stream-enum-int 1000 1000000)))
Infinite Streams • Streams delay evaluation of rest • Can implement infinite streams • No need to get to end • (define (stream-enum-infinite start) • (cons-stream start • (stream-enum-infinite (+ start 1))) • (define posints (stream-enum-infinite 1))
Filter-stream • Contract: filter-stream: • (number -> boolean) stream-of-number -> stream-of-number • Purpose: Create new stream with only elements that pass filter
Filter-stream (define (filter-stream pred stream) (cond ((pred (car stream)) (cons (car stream) (delay (filter-stream pred (force (cdr stream)))))) (else (filter-stream pred (force (cdr stream))))))
Add-streams • Contract: add-streams: • Stream-of-numbers stream-of-numbers -> stream-of-numbers • Purpose: Add numbers from two streams to produce a new streams
Add-streams (define (add-streams s1 s2) (cons (+ (car s1) (car s2)) (delay (add-streams (force (cdr s1)) (force (cdr s2))))))
Creating Natural Numbers • Countably infinite • (define nats (cons 0 (delay (add1-stream nats)))) • (define nats (cons 0 (add-streams ones nats)))
Finding Primes • Idea1: Create prime? predicate and use filter • Idea2: • Remove everything divisible by a smaller prime • Whatever is left should be the prime numbers • Step 1: • (define (remove-divisible n stream) • (filter-stream (lambda (x) (not (divides? x n))) stream))
Seive • (define (seive stream) • (cons (car stream) • (delay (filter-divisible (car stream) • (seive (force (cdr stream)))) • (define nats2 (cons 2 (delay (add1-stream nats2))) • (define primes (seive nats2))