Lecture 18.

Slides:



Advertisements
Similar presentations
Programming with Lists
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
מבוא מורחב - שיעור 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 תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 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.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Functional Programming in Scheme and Lisp.
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 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Additional Scheme examples
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Nondeterministic Evaluation
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
PPL Sequence Interface.
Dynamic Scoping Lazy Evaluation
Lecture #6 מבוא מורחב.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Closures and Streams cs7100(Prasad) L11Clos
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Lecture 2 מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Streams Contract is the same as pairs...
Presentation transcript:

Lecture 18

Infinite streams -- more examples (define (fibgen a b) (cons-stream a (fibgen b (+ a b)))) (define fibs (fibgen 0 1))

Finding all the primes XX 5 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

Remember our sieve? (sieve (2 3 4 . . . )) (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))) (sieve (2 3 4 . . . )) (2 (sieve (stream-filter /2 (2 3 4 . . .)))) (2 (sieve (3 (stream-filter /2 (4 5 . . .))))) (2 3 (sieve (stream-filter /3 (stream-filter /2 (4 5 . . . )))))

Pitfalls (stream-filter odd? (stream-filter even? integers)) (stream-null? (stream-filter odd? (stream-filter even? integers)))

Defining streams implicitely (define ones (cons-stream 1 ones))

(define ones (cons-stream 1 ones)) | GE (define ones (cons 1 (memo-proc (lambda () ones)))) | GE (stream-cdr ones) | GE ones: GE proc: #t p: b: ones 1 already-run: #f result: #f

More implicit definitions of streams (define integers (cons-stream 1 (add-streams ones integers))) (define fibs (cons-stream 0 (cons-stream 1 (add-streams (stream-cdr fibs) fibs)))) fibs (stream-cdr fibs) 1 1 2 3 1 1 1 2 3

More implicit definitions of streams (define (scale-stream stream factor) (stream-map (lambda (x) (* x factor)) stream)) (define double (cons-stream 1 (scale-stream double 2)))

More implicit definitions of streams (define primes (cons-stream 2 (stream-filter prime? (integers-starting-from 3)))) (define (prime? n) (define (iter ps) (cond ((> (square (stream-car ps)) n) true) ((divisible? n (stream-car ps)) false) (else (iter (stream-cdr ps))))) (iter primes))

Replacing state variables with streams (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))

Replacing state variables with streams (define (sqrt-improve guess x) (average guess (/ x guess))) (define (sqrt-stream x) (define guesses (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) guesses))) guesses)

Replacing state variables with streams (define (display-stream s) (stream-for-each display-line s)) (define (display-line x) (newline) (display x)) (display-stream (sqrt-stream 2)) 1. 1.5 1.4166666 1.4142156 . . .

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

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

Speading 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 = (display-stream (euler-transform pi-stream)) 3.1666 3.1333 3.1452 3.1396 3.1427 2.1408

Speading up convergence even further (define (make-tableau transform s) (cons-stream s (make-tableau transform (transform s)))) produces stream of streams: s, (transform s), (transform (transform s)) . . . (define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s)))

Speading up convergence even further (Cont.) (display-stream (accelerated-sequence euler-transform pi-stream)) 4. 3.1666 3.1421 3.1415

Infinite streams of pairs Want to produce the stream of pairs of all integers (i,j) with i  j such that i + j is prime. (1,1) (1,2) (1,4) (2,3) ……. Reduce the problem: (stream-filter (lambda (pair) (prime? (+ (car pair) (cadr pair)))) int-pairs) What should we bind int-pairs to ?

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

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: (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . If S and T are both the integers then this would be int-pairs

Infinite streams of pairs Wishful thinking: Break the desired stream into three pieces 1 2 3 (S1,T1) (S1,T2) (S1,T3) . . . (S2,T2) (S2,T3) . . . (S3,T3) . . . . . . Write a function pairs such that (pairs S T) would be the desired stream.

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

Infinite streams of pairs (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (stream-append (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) (define (stream-append s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (stream-append (stream-cdr s1) s2)))) (define int-pairs (pairs integers integers))

Infinite streams of pairs This implementation is not good !! (stream-car (stream-cdr (stream-cdr . . . . . . int-pairs))))))) .. will never produce a pair with first component different from 1 !! Want to be able to produce any pair by applying stream-cdr enough times.

Infinite streams of pairs (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))))) (define int-pairs (pairs integers integers))