Lecture 2 מבוא מורחב.

Slides:



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

PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב 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 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 한 태숙.
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.
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 מבוא מורחב.
CS314 – Section 5 Recitation 10
Functional Programming
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
The interpreter.
Computing Square Roots
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
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
Lecture 18 Infinite Streams and
Functional Programming
This Lecture Substitution model
CS220 Programming Principles
Dynamic Scoping Lazy Evaluation
Lecture #6 section pages pages72-77
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.
Problem Solving Skill Area 305.1
The Metacircular Evaluator (Continued)
Extended Introduction to Computer Science
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
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
Lecture #6 section pages pages72-77
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
This Lecture Substitution model
Good programming practices
Presentation transcript:

Lecture 2 מבוא מורחב

Review: syntax vs. semantics Syntax Semantics Primitives 23 + * Proc for adding Proc for multiplying Means of Combination (+ 3 17 5) Application of proc to arguments Result = 25 Means of Abstraction (define score 23) Associates score with 23 in environment table מבוא מורחב

An example ==> (define score 23) * + 5 6 - 23 * 3 2 11 11 12 121 מבוא מורחב

Substitution model To Apply a compound procedure to a list of arguments: Replace the formal parameters with the corresponding actual values. Evaluate the body of the procedure with these values. (define square (lambda (x) (* x x))) 1. (square 4) 2. (* 4 4) 3. 16 מבוא מורחב

Review: evaluation of an expression The value of a numeral: number The value of any name: the associated object in the environment The value of a built-in operator: machine instructions to execute To Evaluate a combination (other than special form) or a compound procedure Evaluate all of the sub-expressions in any order Apply the procedure that is the value of the leftmost sub-expression to the arguments (the values of the other sub-expressions) מבוא מורחב

IF special form (if <predicate> <consequent> <alternative>) (if (< 2 3) 2 3) ==> 2 (if (< 2 3) 2 (/ 1 0)) ==> ERROR 2 If the value of <predicate> is #t, Evaluate <consequent> and return it Otherwise Evaluate <alternative> and return it מבוא מורחב

IF is a special form In a general form, we first evaluate all arguments and then apply the function (if <predicate> <consequent> <alternative>) is different: <predicate> determines whether we evaluate <consequent> or <alternative>. We evaluate only one of them Can you see why it has to be that way? מבוא מורחב

Lambda special form lambda syntax (lambda (x y) (+ x y x 2)) 1st operand position: the parameter list (x y) a list of names (perhaps empty) determines the number of operands required 2nd operand position: the body (+ x y x 2) may be any expression not evaluated when the lambda is evaluated evaluated when the procedure is applied מבוא מורחב

Naming procedures An application of an unnamed procedure: ((lambda (x) (* x x)) 4) ==> 16 2. Naming the procedure: (define square (lambda (x) (* x x))) (square 3) 3. Syntactic Sugar: (define (square x) (* x x))

Some examples: (define twice ) (twice 2) ==> 4 (twice 3) ==> 6 (define second ) (second 2 15 3) ==> 15 (second 34 -5 16) ==> -5 (lambda (x) (* 2 x)) (lambda (x y z) y) מבוא מורחב

An example for 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 (applicative order) (/ (+ 5 9) 2) (/ 14 2) if operator is a primitive procedure, 7 replace by result of operation מבוא מורחב

Sum of squares S(n) = 02 + 12 + 22 ………. …… (n-1)2 + n2 Notice that: S(n) = S(n-1) + n2 S(0) = 0 These two properties completely define the function

An algorithm for sum of squares (define sum-squares (lambda (n) (if (= n 0) 0 (+ (sum-squares (- n 1)) (square n))))

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 מבוא מורחב

How to design recursive algorithms Show how to solve big instances, if you know the solution to smaller instances. Wishful thinking: if I could only solve the smaller instance … Solve the “base cases”. For example, for sum of squares: S(n) = S(n-1) + n2 (induction rule) S(0)=0 (base case) מבוא מורחב

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: non-decomposable problem recursive case: larger (decomposable) problem מבוא מורחב

Evaluating (sum-squares 3) in the substitution model, with IF a regular form…. (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))) (if #f 0 (+ (sum-squares 2) 9)) (if #f 0 (+ (if #f 0 (+ (sum-squares 1) 4))) 9)) .. calling (sum-squares 0) …. calling (sum-squares -1) We evaluate all operands. We always call (sum-squares) again. We get an infinite loop…….. OOPS מבוא מורחב

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 מבוא מורחב

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 3. base case מבוא מורחב

SQRT To find an approximation of square root of x: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough G = 1 X = 2 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 מבוא מורחב

(define initial-guess 1.0) (define precision 0.0001) (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)) מבוא מורחב

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

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 מבוא מורחב

Rewriting SQRT (Block structure) (define (sqrt 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)) precision)) (define (improve guess x) (average guess (/ x guess))) (define initial-guess 1.0) (define precision 0.00001) (sqrt-iter initial-guess x)) מבוא מורחב

Lexical Scoping Every variable is: Recognized within the procedure where it is defined (its scope). Not recognized outside it. Allows different procedures to use the same variable name. E.g., different procedures can use the variable name i as a counter. A variable can be used globally within its scope. No need to pass it from one procedure to another. מבוא מורחב

SQRT again, x is used globally. (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 0.00001) (sqrt-iter initial-guess)) מבוא מורחב

Variables scope (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 0.00001) (sqrt-iter initial-guess)) מבוא מורחב

An example (define (proc1 x) (define (proc2 y) (+ x y)) (define (proc3 x) (proc2 x)) (proc3 (* 2 x))) Proc1.x Proc3.x (proc1 4) proc1.x = 4 (proc3 8) proc3.x = 8 (proc2 8) proc2.y = 8 proc2.x=proc1.x=4 12 Local bindings override more global bindings. מבוא מורחב