מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Six compound procedures and higher-order procedures.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
6.001 SICP – September Introduction
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
מבוא מורחב - שיעור 4 1 Lecture 4 Order of Growth Fun with recursion  Fast exponentiation  Hanoi towers.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
Eight compound procedures and higher-order procedures.
Thirteen conditional expressions: letting programs make “decisions”
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Binding names קשירת שמות A occurrence of a name z is bound by the innermost procedure that contains the name and either 1. z is a formal parameter of.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
CS 550 Programming Languages Jeremy Johnson
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Computing Square Roots
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lecture #5 מבוא מורחב.
Functional Programming
Binding names קשירת שמות
This Lecture Substitution model
Lecture #6 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
6.001 SICP Further Variations on a Scheme
Extended Introduction to Computer Science
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Explicit-control evaluator
Material in the textbook Sections to 1.2.1
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
This Lecture Substitution model
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Good programming practices
Presentation transcript:

מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in the code of every recursive algorithm? a recursive case a base case a test Which of the following is a use of lambda? 1. (define fred +) 2. (define (fred x) (+ x x)) only 2: (define fred (lambda (x) (+ x x)))

מבוא מורחב 2 (define (sqrt x) (sqrt-iter 1.0 x)) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess)))

מבוא מורחב 3 Block Structure (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 x))

4 Binding names קשירת שמות A procedure F binds a name x if either 1. x is a formal parameter of F 2. x is a name defined internally in F (define (F x y z) (define (h y) (* y y) (+ (h x) (h y) (h z))) h, x, y, z are bound in F y is bound in h

מבוא מורחב 5 עידון מודל ההצבה When replacing a formal parameter by the corresponding argument, do not substitute for occurences that are bound by an internal definition. An occurrence of a name is bound by the innermost procedure that binds the name and contains the occurrence in its body.

מבוא מורחב 6 refining the substitution model (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 x)) (sqrt 2)

מבוא מורחב 7 (sqrt 2) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 2)

מבוא מורחב 8 Scheme uses Lexical Scoping ערך משתנה חופשי בפונקציה F נקבע על פי מקום הגדרת F. (ולא על פי מקןם הפעלת F) פונקציה המכילה את הגדרת F תקבע את ערכו.

מבוא מורחב 9 (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 x))

מבוא מורחב 10 Taking advantage of Lexical Scoping (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) (sqrt 2)

מבוא מורחב 11 (sqrt 2) (define (good-enough? guess) (< (abs (- (square guess) 2)) 0.001)) (define (improve guess) (average guess (/ 2 guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0) (if (good-enough? 1.0) 1.0 (sqrt-iter (improve 1.0)))

מבוא מורחב 12 (define (h x) (define (f y) (+ x y)) (define (g x) (f x)) (g (* 2 x))) => (h 1) (define (f y) (+ 1 y)) (define (g x) (f x)) (g (* 2 1)) (f 2) (+ 1 2) 3

מבוא מורחב 13 Applicative order evaluation Combination … ( …… ) Evaluate to get the procedure and evaluate to get the arguments If is primitive: do whatever magic it does If is compound: evaluate body with formal parameters replaced by arguments

מבוא מורחב 14 Normal order evaluation Combination … ( …… ) Evaluate to get the procedure and evaluate to get the arguments If is primitive: do whatever magic it does If is compound: evaluate body with formal parameters replaced by arguments

מבוא מורחב 15 Applicative ((lambda (x) (+ x x)) (* 3 4)) ((lambda (x) (+ x x)) 12) ( ) 24 Normal ((lambda (x) (+ x x)) (* 3 4)) (+ (* 3 4) (* 3 4)) ( ) 24 This may matter in some cases: ((lambda (x y) (+ x 2)) 3 (/ 1 0))

מבוא מורחב 16 Compute a b (exp-1 2 2) ==> 4 wishful thinking : base case : a * a (b-1) a 0 = 1 (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))

מבוא מורחב 17 exp-1 is a recursive algorithm In a recursive algorithm, bigger operands => more space (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))(fact 4) (exp-1 2 3) (if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1)))) (* 2 (exp-1 2 2)).. (* 2 (* 2 (exp-1 2 1))).. (* 2 (* 2 (* 2 (exp-1 2 0)))) An iterative algorithm uses constant space

מבוא מורחב 18 Intuition for iterative a b same as you would do if calculating 2 4 by hand: 1. start with 1 (4 more to go) 1. multiply 1 by 2gives 2 (3 more to go) 2. multiply 2 by 2gives 4 (2 more to go) 3. multiply 4 by 2gives 8 (1 more to go) 4. multiply 8 by 2gives 16 (0 more to go) At each step, only need to remember: current product, how many times has yet to multiply Therefore, constant space

מבוא מורחב 19 In scheme: (define (exp-2 a b) (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1)

מבוא מורחב 20 A trace (define (exp-2 a b) (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1) (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter ) (exp-iter ) 81

21 Recursive = pending operations when procedure calls itself Recursive exponentiation: (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1)))))) (exp-1 2 3) (if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1)))) (* 2 (exp-1 2 2)).. (* 2 (* 2 (exp-1 2 1))).. (* 2 (* 2 (* 2 (exp-1 2 0))))(fact 4) operation pending Pending ops make the expression grow continuously

22 Iterative = no pending operations Iterative exponentiation: (define (exp-2 a b) (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1)) (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter ) (exp-iter ) 81 Fixed size because no pending operations no pending operations

מבוא מורחב 23 Summary Iterative algorithms have constant space Using substitution the expression doesn’t grow. Iterative algorithms have no pending operations when the procedure calls itself How to develop an iterative algorithm figure out a way to accumulate partial answers translate rules into scheme code

מבוא מורחב 24 Rules for evaluation ``Elementary expressions'' are left alone: Elementary expressions are Numerals initial names of primitive procedures lambda expressions, naming procedures A name bound by DEFINE: Rewrite the name as the value it is associated with by the definition IF: If the evaluation of the predicate expression terminates in a non-false value then rewrite the IF expression as the value of the consequent, otherwise, rewrite the IF expression as the value of the alternative. Combination: Evaluate the operator expression to get the procedure, and evaluate the operand expressions to get the arguments, If the operator names a primitive procedure, do whatever magic the primitive procedure does. If the operator names a compound procedure, evaluate the body of the compound procedure with the arguments substituted for the formal parameters in the body.

מבוא מורחב 25 Orders of growth of processes Suppose n is a parameter that measures the size of a problem Let R(n) be the amount of resources needed to compute a procedure of size n. Two common resources are space, measured by the number of deferred operations, and time, measured by the number of primitive steps. Want to estimate the “order of growth” of R(n): R 1 (n)=100n 2 R 2 (n)=2n 2 +10n+2 R 3 (n) = n 2 Are all the same in the sense that if we multiply the input by a factor of 2, the resource consumption increase by a factor of 4

מבוא מורחב 26 Orders of growth of processes We say R(n) has order of growth  f(n)  if there are constants k 1 and k 2 such that k 1 f(n) <= R(n) <= k 2 f(n) for all n R(n)  O(f(n)) if there is a constant k such that R(n) <= k f(n) for all n R(n)   (f(n)) if there is a constant k such that k f(n) <= R(n) for all n   x x 100n 2  O(n) 100n 2   (n) 100n 2   (n) 100n 2   (n 2 ) k 1, k 2, k  0

מבוא מורחב 27 Resources consumed by exp-1 In a recursive algorithm, bigger operands => more space (define exp-1 (lambda (a b) (if (= b 0) 1 (* a (exp-1 a (- b 1))))))(fact 4) (exp-1 2 3) (if (= 3 0) 1 (* 2 (exp-1 2 (- 3 1)))) (* 2 (exp-1 2 2)).. (* 2 (* 2 (exp-1 2 1))).. (* 2 (* 2 (* 2 (exp-1 2 0)))) Space  b  Time  b 

מבוא מורחב 28 Resources consumed by exp-2 (define (exp-2 a b) (define (exp-iter a b product) (if (= b 0) product (exp-iter a (- b 1) (* a product)))) (exp-iter a b 1) (exp-2 3 4) (exp-iter 3 4 1) (exp-iter 3 3 3) (exp-iter 3 2 9) (exp-iter ) (exp-iter ) 81 Space  1  Time  b 