1 / 14

Streams: Infinite Data

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.

semah
Download Presentation

Streams: Infinite Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 11500 Introduction to Computer Programming November 18, 2002 Streams:Infinite Data

  2. Roadmap • Recap: Streams Basics • Streams • Streams and Abstraction • Map-stream • Filter • Natural numbers: • Add1-stream, Add-streams • Finding Primes • Seive of Eratosthenes

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

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

  5. Stream-enum-int • Contract: number number -> stream • Produce a stream of numbers between a & b

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

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

  8. Filter-stream • Contract: filter-stream: • (number -> boolean) stream-of-number -> stream-of-number • Purpose: Create new stream with only elements that pass filter

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

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

  11. Add-streams (define (add-streams s1 s2) (cons (+ (car s1) (car s2)) (delay (add-streams (force (cdr s1)) (force (cdr s2))))))

  12. Creating Natural Numbers • Countably infinite • (define nats (cons 0 (delay (add1-stream nats)))) • (define nats (cons 0 (add-streams ones nats)))

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

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

More Related