מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages 316-352 definitions file on web 2.

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

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.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
מבוא מורחב - שיעור 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 ((>
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
Extended Introduction to CS Preparation for Final Exam.
Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Extended Introduction to CS Preparation for Final Exam.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
SICP Infinite streams – using lazy evaluation Beyond Scheme – designing language variants: Streams – an alternative programming style!
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
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 한 태숙.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 12. Outline Streams Infinite streams Stream implementation Questions from exams 2.
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.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
6.001 Final Exam Review Spring 2005 By Gerald Dalley.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
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.
Streams Review A Review of Streams Mark Boady. What Are Steams? Delayed lists We pretend that the stream is a list In reality we only know the next value,
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Additional Scheme examples
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
Lecture 18 Infinite Streams and
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Streams… …row, row, row your boat….
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13

Streams 3.5, pages definitions file on web 2

cons, car, cdr (define s (cons 9 (begin (display 7) 5))) -> prints 7 The display command is evaluated while evaluating the cons. (car s) -> 9 (cdr s) -> 5 3

cons-stream, stream-car, stream-cdr (define s (cons-stream 9 (begin (display 7) 5))) Due to the delay of the second argument, cons-stream does not activate the display command (stream-car s) -> 9 (stream-cdr s) -> prints 7 and returns 5 stream-cdr activates the display which prints 7, and then returns 5. 4

List enumerate (define (enumerate-interval low high) (if (> low high) nil (cons low (enumerate-interval (+ low 1) high)))) (enumerate-interval 2 8) -> ( ) (car (enumerate-interval 2 8)) -> 2 (cdr (enumerate-interval 2 8)) -> ( ) 5

Stream enumerate (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (stream-enumerate-interval 2 8) -> (2. # ) (stream-car (stream-enumerate-interval 2 8)) -> 2 (stream-cdr (stream-enumerate-interval 2 8)) -> (3. # ) 6

List map (map ) (define (map proc s) (if (null? s) nil (cons (proc (car s)) (map proc (cdr s))))) (map square (enumerate-interval 2 8)) -> ( ) 7

Stream map (map ) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s)) ))) (stream-map square (stream-enumerate-interval 2 8)) -> (4. # ) 8

List of squares (define squares (map square (enumerate-interval 2 8))) squares -> ( ) (car squares) -> 4 (cdr squares) -> ( ) 9

Stream of squares (define stream-squares (stream-map square (stream-enumerate-interval 2 8))) stream-squares -> (4. # ) (stream-car stream-squares) -> 4 (stream-cdr stream-squares) -> (9. # ) 10

List reference (define (list-ref s n) (if (= n 0) (car s) (list-ref (cdr s) (- n 1)))) (define squares (map square (enumerate-interval 2 8))) (list-ref squares 3) -> 25 11

Stream reference (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define stream-squares (stream-map square (stream-enumerate-interval 2 8))) (stream-ref stream-squares 3) -> 25 12

List filter (filter ) (define (filter pred s) (cond ((null? s) nil) ((pred (car s)) (cons (car s) (filter pred (cdr s)))) (else (filter pred (cdr s))))) (filter even? (enumerate-interval 1 20)) -> ( ) 13

Stream filter (stream-filter ) (define (stream-filter pred s) (cond ((stream-null? s) the-empty-stream) ((pred (stream-car s)) (cons-stream (stream-car s) (stream-filter pred (stream-cdr s)))) (else (stream-filter pred (stream-cdr s))) ))) (stream-filter even? (stream-enumerate-interval 1 20)) -> (2. # ) 14

Generalized list map (generalized-map … ) (define (generalized-map proc. arglists) (if (null? (car arglists)) nil (cons (apply proc (map car arglists)) (apply generalized-map (cons proc (map cdr arglists)))))) (generalized-map + squares squares squares) -> ( ) 15

Generalized stream map (generalized-stream-map … ) (define (generalized-stream-map proc. argstreams) (if (stream-null? (car argstreams)) the-empty-stream (cons-stream (apply proc (map stream-car argstreams)) (apply generalized-stream-map (cons proc (map stream-cdr argstreams)))))) (generalized-stream-map + stream-squares stream-squares stream-squares) -> (12. # ) 16

List for each (define (for-each proc s) (if (null? s) 'done (begin (proc (car s)) (for-each proc (cdr s))))) 17

Stream for each (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car s)) (stream-for-each proc (stream-cdr s))))) useful for viewing (finite!) streams (define (display-stream s) (stream-for-each display s)) (display-stream (stream-enumerate-interval 1 20)) -> prints 1 … 20 done 18

Lists (define sum 0) (define (acc x) (set! sum (+ x sum)) sum) (define s (map acc (enumerate-interval 1 20))) s -> ( ) sum -> 210 (define y (filter even? s)) y -> ( ) sum -> 210 (define z (filter (lambda (x) (= (remainder x 5) 0)) s)) z -> ( ) sum ->

(list-ref y 7) -> 136 sum -> 210 (display z) -> prints ( ) sum ->

Streams (define sum 0) (define (acc x) (set! sum (+ x sum)) sum) (define s (stream-map acc (stream-enumerate-interval 1 20))) s -> (1. # )sum -> 1 (define y (stream-filter even? s)) y -> (6. # )sum -> 6 (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) s)) z -> (10. # )sum -> 10 21

(stream-ref y 7) -> 136 sum -> 136 (display-stream z) -> prints done sum ->

Defining streams implicitly by delayed evaluation Suppose we needed an infinite list of Dollars. We can (define bill-gates (cons-stream ‘dollar bill-gates)) If we need a Dollar we can take the car (stream-car bill-gates) -> dollar The cdr would still be an infinite list of Dollars. (stream-cdr bill-gates)->(dollar. # ) 23

24 Infinite Streams Formulate rules defining infinite series wishful thinking is key 24

1,ones (define ones (cons-stream 1 ones)) 1,1,1,… = ones = 25

2,twos (define twos (cons-stream 2 twos)) ones + ones adding two infinite series of ones (define twos (stream-map + ones ones)) 2 * ones element-wise operations on an infinite series of ones (define twos (stream-map (lambda (x) (* 2 x)) ones)) or (+ x x) 2,2,2,… = twos = 26

1,2,3,… = integers = 1,ones + integers 1,1,1… 1,2,3,… 2,3,4,… (define integers (cons-stream 1 (stream-map + ones integers))) + 27

0,1,1,2,3,… = fibs = 0,1,fibs + (fibs from 2 nd position) 0,1,1,2,… 1,1,2,3,… 1,2,3,5,… (define fibs (cons-stream 0 (cons-stream 1 (stream-map + fibs (stream-cdr fibs))))) + 28

1,doubles + doubles 1,2,4,8,… 2,4,8,16,… (define doubles (cons-stream 1 (stream-map + doubles doubles))) 1,2,4,8,… = doubles = + 29

1,2 * doubles (define doubles (cons-stream 1 (stream-map (lambda (x) (* 2 x)) doubles))) or (+ x x) 1,2,4,8,… = doubles = 30

1,factorials * integers from 2 nd position 1, 1*2, 1*2*3,… 2, 3, 4,… 1*2,1*2*3,1*2*3*4,… (define factorials (cons-stream 1 (stream-map * factorials (stream-cdr integers)))) 1,1x2,1x2x3,... = factorials = x 31

(1),(1 2),(1 2 3),… = runs = (1), append runs with a list of integers from 2 nd position (1), (1 2), (1 2 3),… (2), (3), (4),… (1 2),(1 2 3),( ),… (define runs (cons-stream (list 1) (stream-map append runs (stream-map list (stream-cdr integers))))) append 32

a0,a0+a1,a0+a1+a2,… = partial sums = a0,partial sums + (stream from 2 nd pos) a0, a0+a1, a0+a1+a2,… a1, a2, a3,… a0+a1,a0+a1+a2,a0+a1+a2+a3,… (define (partial-sums a) (cons-stream (stream-car a) (stream-map + (partial-sums a) (stream-cdr a)))) + 33

34 Partial Sums (cont.) (define (partial-sums a) (define sums (cons-stream (stream-car a) (stream-map + sums (stream-cdr a)))) sums) This implementation is more efficient since it uses the stream itself rather than recreating it recursively 34

Approximating the natural logarithm of 2 1,-1,1,-1,… (define alternate (cons-stream 1 (stream-map - alternate))) 1/1,-1/2,1/3,… (define ln2-series (stream-map / alternate integers)) 35

Approximating the natural logarithm of 2 (define ln2 (partial-sums ln2-series)) 1,1/2,5/6,7/12,… using Euler’s sequence acceleration (define ln2-euler (euler-transform ln2)) 7/10,29/42,25/36,457/660,… using super acceleration (define ln2-accelerated (accelerated-sequence euler-transform ln2)) 1,7/10,165/238, / ,… 36

Power series 37

Power series The series is represented as the stream whose elements are the coefficient a0,a1,a2,a3… 38

Power series integral The integral of the series is the series where c is any constant 39

Power series integral Input: representing a power series Output: coefficients of the non-constant term of the integral of the series (define (integrate-series a) (stream-map / a integers)) 40

Exponent series The function is its own derivative and the integral of are the same except for the constant term According to this rule, a definition of the exponent series is: (define exp-series (cons-stream 1 (integrate-series exp-series))) which results in 1,1,1/2,1/6,1/24,1/120… as expected 41

Sine and cosine series The derivate of sine is cosine The derivate of cosine is (- sine) (define cosine-series (cons-stream 1 (stream-map – (integrate-series sine-series)))) (define sine-series (cons-stream 0 (integrate-series cosine-series))) Which results in cosine-series: 1,0,-1/2,0,1/24,… sine-series: 0,1,0,-1/6,0,1/120,… As expected 42

Repeat Input: procedure f of one argument, number of repetitions Output: f*…*f, n times (define (repeated f n) (if (= n 1) f (compose f (repeated f (- n 1))))) (define (compose f g) (lambda (x) (f (g x)))) 43

Repeat stream f,f*f,f*f*f,… = repeat = f,compose f,f,f,… with repeat (define f-series (cons-stream f f-series)) (define stream-repeat (cons-stream f (stream-map compose f-series stream-repeat))) We would like f to be a parameter 44

Repeat stream f,f*f,f*f*f,… = repeat = f,compose f,f,f,… with repeat (define (repeated f) (define f-series (cons-stream f f-series)) (define stream-repeat (cons-stream f (stream-map compose f-series stream-repeat))) stream-repeat) 45

Interleave 1,1,1,2,1,3,1,4,1,5,1,6,… (interleave ones integers) s0,t0,s1,t1,s2,t2,… interleave = s0,interleave (t, s from 2 nd position) (define (interleave s t) (if (stream-null? s) t (cons-stream (stream-car s) (interleave t (stream-cdr s))))) 46