Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Programming with Lists
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
Closures and Streams More on Evaluations CS784(pm)1.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS314 – Section 5 Recitation 9
Lecture #5 מבוא מורחב.
CS314 – Section 5 Recitation 10
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
Nondeterministic Evaluation
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
PPL Sequence Interface.
Lecture 18.
The Metacircular Evaluator
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
Lecture 2 מבוא מורחב.
Streams Contract is the same as pairs...
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16

Finite Streams (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (define s (stream-enumerate-interval 1 1000000000)) מבוא מורחב - שיעור 16

Infinite streams Since streams are delayed, we are not limited to finite streams!! Some objects are more naturally infinite מבוא מורחב - שיעור 16

The integers (define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1)) מבוא מורחב - שיעור 16

Integers not divisible by 7 (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (stream-filter (lambda (x) (not (divisible? x 7))) integers)) (stream-ref no-sevens 100)  117 מבוא מורחב - שיעור 16

Fibonacci sequence (define (fib-seq-from a b) (cons-stream a (fib-seq-from b (+ a b)))) (define fib-seq (fib-seq-from 0 1)) מבוא מורחב - שיעור 16

Fibonacci sequence (define (fib-seq-from a b) (cons-stream a (fib-seq-from b (+ a b)))) (define fib-seq (fib-seq-from 0 1)) fib-seq (0 . #<struct:promise>[fib-seq-from 1 1]) (stream-ref fib-seq 3) (stream-ref (stream-cdr fib-seq) 2) (stream-ref (fib-seq-from 1 1) 2) (stream-ref (cons 1 (delay (fib-seq-from 1 2))) 2) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 1 2)) 1) (stream-ref (fib-seq-from 1 2) 1) (stream-ref (cons 1 (delay (fib-seq from 2 3))) 1) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 2 3)))) 0) (stream-ref (fib-seq-from 2 3) 0) (stream-ref (cons 2 (delay (fib-seq-from 3 5))) 0) 2 מבוא מורחב - שיעור 16

Finding all the primes using the sieve of Eratosthenes 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 60 69 68 67 66 65 64 63 62 61 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 XX X 3 XX 7 XX X 2 XX 5 מבוא מורחב - שיעור 16

The sieve of Eratosthenes using streams (define (sieve str) (cons-stream (stream-car str) (sieve (stream-filter (lambda (x) (not (divisible? x (stream-car str)))) (stream-cdr str))))) (define primes (sieve (stream-cdr integers))) מבוא מורחב - שיעור 16

The integers, again (implicit version) We saw the definition: (define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1)) An alternative definition: (define integers (cons-stream 1 (stream-map (lambda (x) (+ x 1)) integers)) (integers) 1 2 ..... input to stream-map integers 1 2 3 ..... result: element + 1 מבוא מורחב - שיעור 16

The integers – another way (define ones (cons-stream 1 ones)) (define integers (cons-stream 1 (add-streams ones integers)) (define (add-streams s1 s2) (stream-map + s1 s2)) A direct implementation of add-streams: (define (add-streams s1 s2) (cons-stream (+ (stream-car s1) (stream-car s2)) (add-streams (stream-cdr s1) (stream-cdr s2)))) מבוא מורחב - שיעור 16

Implicit definition of the Fibonacci numbers (define fibs (cons-stream 0 (cons-stream 1 (add-streams (stream-cdr fibs) fibs)))) 1 1 2 3 fibs (stream-cdr fibs) 1 1 1 2 3 מבוא מורחב - שיעור 16

More implicit definitions (define (scale-stream str factor) (stream-map (lambda (x) (* x factor)) str) (define double (cons-stream 1 (scale-stream double 2))) 1 (scale-factor double 2) double 2 4 8 מבוא מורחב - שיעור 16

The stream of primes – another way (define primes (cons-stream 2 (stream-filter prime? (integers-from 3)))) (define (prime? n) (define (iter ps) (cond ((> (square (stream-car ps)) n) #t) ((divisible? n (stream-car ps)) #f) (else (iter (stream-cdr ps))))) (iter primes) A recursive definition. Works because the primes needed to check primality of the next number are always ready. מבוא מורחב - שיעור 16

Formulating iterations as stream processes (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (sqrt-improve guess)))) (sqrt-iter 1.0)) מבוא מורחב - שיעור 16

Formulating iterations as stream processes (cont’d) (define (sqrt-stream x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (sqrt-improve guess x) (average guess (/ x guess))) (define guesses (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) guesses))) ________________________________________________ ________________________________________________) (stream-car (stream-filter (lambda(x) (good-enough? x)) guesses))) מבוא מורחב - שיעור 16

Formulating iterations as stream processes (Cont.)  4 = 1 3 5 7 - + . . . (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) (pi-summands 1) (1 (stream-map - (pi-summands 3))) (1 (stream-map - (1/3 (stream-map - (pi-summands 5)))) (1 -1/3 (stream-map - (stream-map - (pi-summands 5)))) (1 -1/3 +1/5 (stream-map - (stream-map - (stream-map - (pi-summands 7))))) מבוא מורחב - שיעור 16

Replacing state variables with streams (Cont.)  4 = 1 3 5 7 - + . . . (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) (define pi-stream (scale-stream (partial-sums (pi-summands 1)) 4)) (display-stream pi-stream) 4. 2.6666 3.4666 2.8952 3.3396 2.9760 S = S0,S0+S1,S0+S1+S2,… converges very slowly! מבוא מורחב - שיעור 16

Speeding up convergence (define (euler-transform s) (let ((s0 (stream-ref s 0)) (s1 (stream-ref s 1)) (s2 (stream-ref s 2))) (cons-stream (- s2 (/ (square (- s2 s1)) (+ s0 (* -2 s1) s2))) (euler-transform (stream-cdr s))))) Sn+1 (Sn+1 - Sn)2 Sn-1 - 2Sn + Sn+1 - S’n = מבוא מורחב - שיעור 16

Speeding Up Convergence (Cont.) (display-stream (euler-transform pi-stream)) 3.1666 3.1333 3.1452 3.1396 3.1427 3.1408 מבוא מורחב - שיעור 16

Speeding up convergence even further (define (make-tableau transform s) (cons-stream s (make-tableau transform (transform s)))) produces a stream of streams: s, (transform s), (transform (transform s)) . . . S00 S01 S02 S03 … S10 S11 S12 … S20 S21 … S30 … מבוא מורחב - שיעור 16

Speeding up convergence even further (Cont.) (define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s))) S00 S01 S02 S03 . . S10 S11 S12 . . . S20 S21 ,. . . . . . (display-stream (accelerated-sequence euler-transform pi-stream)) 4. 3.1666… 3.1421… 3.1415… מבוא מורחב - שיעור 16

Infinite streams of pairs Want to produce the stream of pairs of all integers (i,j) with i  j and bind it to int-pairs. Abstraction: Let’s solve an even more general problem. Given two streams S=(S1, S2 . . . . .) T=(T1, T2 . . . . .) Consider the infinite rectangular array (S1,T1) (S1,T2) (S1,T3) . . . (S2,T1) (S2,T2) (S2,T3) . . . (S3,T1) (S3,T2) (S3,T3) . . . . . . . . . . . . מבוא מורחב - שיעור 16

Infinite streams of pairs (S1,T1) (S1,T2) (S1,T3) . . . (S2,T1) (S2,T2) (S2,T3) . . . (S3,T1) (S3,T2) (S3,T3) . . . . . . . . . . . . Wish to produce all pairs that lie on or above the diagonal: j (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . i If S and T are both the integers then this would be int-pairs מבוא מורחב - שיעור 16

Infinite streams of pairs (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . 1 2 3 Write a function pairs such that (pairs s t) would be the desired stream. Wishful thinking: Break the desired stream into three pieces מבוא מורחב - שיעור 16

Infinite streams of pairs (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . 1 2 3 Produce by (list (stream-car s) (stream-car t)) 1 Produce by (pairs (stream-cdr s) (stream-cdr t)) 3 Produce by (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) 2 מבוא מורחב - שיעור 16

Infinite streams of pairs (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (combine-in-some-way (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) 1 How do we combine? 2 3 (define (stream-append s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (stream-append (stream-cdr s1) s2)))) מבוא מורחב - שיעור 16

Infinite streams of pairs (cont’d) (define int-pairs (pairs integers integers)) (display-stream int-pairs) (1 1) (1 2) (1 3) (1 4) … will never produce a pair with first component different from 1 !! stream-append does not work when the first stream in infinite We want to be able to produce any pair by applying stream-cdr enough times. מבוא מורחב - שיעור 16

Infinite streams of pairs (cont’d) (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (interleave (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) מבוא מורחב - שיעור 16

Infinite streams of pairs (cont’d) (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (interleave (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define int-pairs (pairs integers integers)) (display-stream int-pairs) (1 1) (1 2) (2 2) (1 3) (2 3) (1 4) (3 3) (1 5)… מבוא מורחב - שיעור 16

Isn’t there a simpler solution? (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . 1 2 Produce by (stream-map (lambda (x) (list (stream-car s) x)) t) 1 Produce by (pairs (stream-cdr s) (stream-cdr t)) 2 (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t))))) מבוא מורחב - שיעור 16

Would this work? Infinite loop!!! pairs calls interleave with pairs as a parameter, which causes pairs to be evaluated (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . 1 2 (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t))))) (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) Because there is no pre-computed “car”, interleave is called again…which calls pairs and so on. מבוא מורחב - שיעור 16

The generalized stream-map (multiple streams) (define (stream-map proc . argstreams) (if (or (null? argstreams) (stream-null? (car argstreams))) the-empty-stream (cons-stream (apply proc (map stream-car argstreams)) (apply stream-map (cons proc (map stream-cdr argstreams)))))) Does it work for finite/infinite streams? What if some are finite and others infinite? What about finite streams with different lengths? מבוא מורחב - שיעור 16