מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

Slides:



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

מבוא מורחב 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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב למדעי המחשב בשפת 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:
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב 1 Lecture 3 Material in the textbook on Pages of 2nd Edition Sections to
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
מבוא מורחב 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.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
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.
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 한 태숙.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
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 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Computing Square Roots
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
CS21b: Structure and Interpretation
6.001 SICP Data abstractions
Higher-Order Procedures
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 מבוא מורחב.
The Metacircular Evaluator
Functional Programming
This Lecture Substitution model
The Metacircular Evaluator
Lecture #6 section pages pages72-77
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.
The Metacircular Evaluator (Continued)
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.
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Rules of evaluation The value of a number is itself.
Good programming practices
Presentation transcript:

מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)

מבוא מורחב - שיעור Evaluation of An Expression To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values To Evaluate a combination: (other than special form) a.Evaluate all of the sub-expressions in any order b.Apply the procedure that is the value of the leftmost sub- expression to the arguments (the values of the other sub- expressions) The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment

מבוא מורחב - שיעור 2 3 lambda: (lambda (x y) (+ x y x 2)) 1st operand position: the parameter list (x y)  a list of names (perhaps empty) 2nd operand position: the body (+ x y x 2)  may be any sequence of expressions The value of a lambda expression is a compound procedure. Reminder: Lambda special form

מבוא מורחב - שיעור 2 4 Evaluating expressions To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values ==> ((lambda(x)(* x x)) 5) Proc(x)(* x x) 5 (* 5 5) 25

מבוא מורחב - שיעור Using Abstractions ==> (square 3) 9 ==> (+ (square 3) (square 4)) ==> (define square (lambda(x)(* x x))) (* 3 3) (* 4 4) Environment Table NameValue squareProc (x)(* x x)

מבוא מורחב - שיעור Yet More Abstractions ==> (define f (lambda(a) (sum-of-two-squares (+ a 3) (* a 3)))) ==> (sum-of-two-squares 3 4) 25 ==> (define sum-of-two-squares (lambda(x y)(+ (square x) (square y)))) Try it out…compute (f 3) on your own

מבוא מורחב - שיעור Syntactic Sugar for naming procedures (define square (lambda (x) (* x x)) (define (square x) (* x x)) Instead of writing: We can write:

מבוא מורחב - שיעור (define second ) (second ) ==> 15 (second ) ==> -5 Some examples: (lambda (x) (* 2 x)) (lambda (x y z) y) Using “syntactic sugar”: (define (twice x) (* 2 x)) Using “syntactic sugar”: (define (second x y z) y) (define twice ) (twice 2) ==> 4 (twice 3) ==> 6

מבוא מורחב - שיעור Lets not forget The Environment ==> (define x 8) ==> (+ x 1) 9 ==> (define x 5) ==> (+ x 1) 6 The value of (+ x 1) depends on the environment!

מבוא מורחב - שיעור 2 10 Using the substitution model (define square (lambda (x) (* x x))) (define average (lambda (x y) (/ (+ x y) 2))) (average 5 (square 3)) (average 5 (* 3 3)) (average 5 9)first evaluate operands, then substitute (/ (+ 5 9) 2) (/ 14 2)if operator is a primitive procedure, 7replace by result of operation

מבוא מורחב - שיעור 2 11 Booleans Two distinguished values denoted by the constants #t and #f The type of these values is boolean ==> (< 2 3) #t ==> (< 4 3) #f

מבוא מורחב - שיעור 2 12 Values and types Values have types. For example: In scheme almost every expression has a value Examples: 1)The value of 23 is 23 2)The value of + is a primitive procedure for addition 3)The value of (lambda (x) (* x x)) is the compound procedure proc (x) (* x x) 1)The type of 23 is numeral 2)The type of + is a primitive procedure 3)The type of proc (x) (* x x) is a compound procedure 4)The type of (> x 1) is a boolean (or logical)

מבוא מורחב - שיעור 2 13 No Value? In scheme almost every expression has a value Why almost? Example : what is the value of the expression (define x 8) In scheme, the value of a define expression is “undefined”. This means “implementation-dependent” Dr. Scheme does not return (print) any value for a define expression. Other interpreters may act differently.

מבוא מורחב - שיעור 2 14 More examples ==> (define x 8) Name Value Environment Table 8x ==> (define x (* x 2)) ==> x 16 ==> (define x y) reference to undefined identifier: y ==> (define + -) # + ==> (+ 2 2) 0

מבוא מורחב - שיעור 2 15 The IF special form ERROR2 (if ) If the value of is #t, Evaluate and return it Otherwise Evaluate and return it (if ( 2 (if (

מבוא מורחב - שיעור 2 16 IF is a special form In a general form, we first evaluate all arguments and then apply the function (if ) is different: determines whether we evaluate or. We evaluate only one of them !

מבוא מורחב - שיעור 2 17 Using the substitution model (define square (lambda (x) (* x x))) (define average (lambda (x y) (/ (+ x y) 2))) (average 5 (square 3)) (average 5 (* 3 3)) (average 5 9)first evaluate operands, then substitute (/ (+ 5 9) 2) (/ 14 2)if operator is a primitive procedure, 7replace by result of operation

מבוא מורחב - שיעור 2 18 Recursive Procedures How to create a process of unbounded length? Needed to solve more complicated problems. Start with a simple example.

מבוא מורחב - שיעור 2 19 Example: Sum of squares S(n) = ………. …… (n-1) 2 + n 2 Notice that: S(n) = S(n-1) + n 2 S(0) = 0 These two properties completely define the function S(n-1) Wishful thinking: if I could only solve the smaller instance …

מבוא מורחב - שיעור 2 20 An algorithm for computing sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))

מבוא מורחב - שיעור 2 21 Evaluating (sum-squares 3) (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) (sum-squares 3) (if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3))) (+ (sum-squares (- 3 1)) (square 3)) (+ (sum-squares (- 3 1)) (* 3 3)) (+ (sum-squares (- 3 1)) 9) (+ (sum-squares 2) 9) (+ (if (= 2 0) 0 (+ (sum-squares (- 2 1)) (square 2))) 9) … (+ (+ (sum-squares 1) 4) 9) … (+ (+ (+ (sum-squares 0) 1) 4) 9) (+ (+ (+ (if (= 0 0) 0 (+ (sum-squares (- 0 1)) (square 0))) 1) 4) 9) (+ (+ (+ 0 1) 4) 9) … 14 What would have happened if ‘ if ’ was a function ?

מבוא מורחב - שיעור 2 22 (sum-squares 3) (if (= 3 0) 0 (+ (sum-squares (- 3 1)) (square 3))) (if #f 0 (+ (sum-squares 2) 9)) (if #f 0 (+ (if #f 0 (+ (sum-squares 1) 4)) 9)) (if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (sum-squares 0) 1)) 4)) 9)) (if #f 0 (+ (if #f 0 (+ (if #f 0 (+ (if #t 0 (+ (sum-squares -1) 0)) 1)) 4)) 9)).. Evaluating (sum-squares 3) with IF as regular form (define (sum-squares n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n)))) We evaluate all operands. We always call (sum-squares) again. We get an infinite loop…….. OOPS

מבוא מורחב - שיעור 2 23 General form of recursive algorithms test, base case, recursive case (define sum-sq (lambda (n) (if (= n 0) ; test for base case 0 ; base case (+ (sum-sq (- n 1)) (square n)) ; recursive case ))) base case: small (non-decomposable) problem recursive case: larger (decomposable) problem at least one base case, and at least one recursive case.

מבוא מורחב - שיעור 2 24 Another example of a recursive algorithm even? (define even? (lambda (n) (not (even? (- n 1))) ; recursive case ))) (if (= n 0) ; test for base case #t ; base case

מבוא מורחב - שיעור 2 25 Short summary Design a recursive algorithm by 1. Solving big instances using the solution to smaller instances. 2. Solving directly the base cases. Recursive algorithms have 1. test 2. recursive case(s) 3. base case(s)

מבוא מורחב - שיעור 2 26 Block Structure Lets write a procedure that given x, y, and z computes f(x,y,z) = (x+y) 2 + (x+z) 2 (define (sum-and-square x y) (square (+ x y))) (define (f x y z) (+ (sum-and-square x y) (sum-and-square x z)))

מבוא מורחב - שיעור 2 27 Block structure (cont.) (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) Lets write a procedure that given inputs x, y, and z, computes f(x,y,z) = (x+y) 2 + (x+z) 2 while keeping sum-and-square private to f (hidden from the outside world):

מבוא מורחב - שיעור 2 28 We still need to clarify the substitution model.. (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) ==> (f 1 2 3) (define (sum-and-square 1 2) (square (+ 1 2))) (+ (sum-and-square 1 2) (sum-and-square 1 3)))

מבוא מורחב - שיעור 2 29 Bounded variables and scope A procedure definition binds its formal parameters The scope of the formal parameter is the body of the procedure. This is called lexical scoping (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) x,y,z x,y

מבוא מורחב - שיעור 2 30 Evaluation of An Expression (refined) To Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters replaced by the corresponding actual values. Do not substitute for occurrences that are bound by an internal definition. To Evaluate a combination: (other than special form) a.Evaluate all of the sub-expressions in any order b.Apply the procedure that is the value of the leftmost sub- expression to the arguments (the values of the other sub- expressions) The value of a numeral: number The value of a built-in operator: machine instructions to execute The value of any name: the associated object in the environment

מבוא מורחב - שיעור 2 31 The refined substitution model (define (f x y z) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square x y) (sum-and-square x z))) ==> (f 1 2 3) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square 1 2) (sum-and-square 1 3)))

מבוא מורחב - שיעור 2 32 The refined substitution model ==> (f 1 2 3) (define (sum-and-square x y) (square (+ x y))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) (+ (sum-and-square 1 2) (sum-and-square 1 3))) Sum-and-square Proc (x y) (square (+ x y))

מבוא מורחב - שיעור 2 33 Computing SQRT: A Numeric Algorithm To find an approximation of square root of x, use the following recipe: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough G = 1X = 2 X/G = 2G = ½ (1+ 2) = 1.5 X/G = 4/3G = ½ (3/2 + 4/3) = 17/12 = X/G = 24/17G = ½ (17/ /17) = 577/408 =

מבוא מורחב - שיעור 2 34 (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)) precision)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt x) (sqrt-iter initial-guess x)) (define initial-guess 1.0) (define precision )

מבוא מורחב - שיעור 2 35 Good programming Style 1. Divide the task to well-defined, natural, and simple sub-tasks. E.g: good-enough? and improve. Rule of thumb : If you can easily name it, it does a well-defined task. 2. Use parameters. E.g.: precision, initial-guess. 3. Use meaningful names.

מבוא מורחב - שיעור 2 36 Procedural abstraction It is better to: Export only what is needed Hide internal details. The procedure SQRT is of interest for the user. The procedure improve-guess is an internal detail. Exporting only what is needed leads to: A clear interface Avoids confusion

מבוא מורחב - שיעור 2 37 Rewriting SQRT (Block structure) (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) precision)) (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))) (define initial-guess 1.0) (define precision ) (sqrt-iter initial-guess x))

מבוא מורחב - שיעור 2 38 Further improving sqrt Note that in every application of sqrt we substitute for x the same value in all subsequent applications of compound procedures ! Therefore we do not have to explicitly pass x as a formal variable to all procedures. Instead, can leave it unbounded (“free”).

מבוא מורחב - שיעור 2 39 SQRT again, taking advantage of the refined substitution model (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) precision)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision ) (sqrt-iter initial-guess))

מבוא מורחב - שיעור 2 40 SQRT (cont.) ==>(sqrt 2) (define (good-enough? guess) (< (abs (- (square guess) 2)) precision)) (define (improve guess) (average guess (/ 2 guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (define initial-guess 1.0) (define precision ) (sqrt-iter initial-guess))

מבוא מורחב - שיעור 2 41 Lexical Scoping - again The lexical scoping rules means that the value of a variable which is unbounded (free) in a procedure f is taken from the procedure in which f was defined. It is also called static scoping

מבוא מורחב - שיעור 2 42 Another example for lexical scope (define (proc1 x) (define (proc2 y) (+ x y)) (define (proc3 x) (proc2 x)) (proc3 (* 2 x))) Proc3.x Proc1.x (proc1 4) proc1.x = 4 (proc3 8) proc3.x = 8 (proc2 8) proc2.y = 8 proc2.x=proc1.x=4 12