PPL Lecture 3 Slides by Yaron Gonen,

Slides:



Advertisements
Similar presentations
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
Advertisements

Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
מבוא מורחב 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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Practice session 3 תחביר ממשי ( קונקרטי ) ותחביר מופשט ( אבסטרקטי ) שיטות חישוב : Applicative & Normal Evaluation Partial Evaluation.
Going Functional Primož Gabrijelčič. Functional programming.
Practice session 3.  תחביר ממשי ( קונקרטי ) ותחביר מופשט ( אבסטרקטי )  שיטות חישוב : Applicative & Normal Evaluation.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
9-October-2002cse MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
PPL Lecture 4 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
PPL Lecture 4 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
PPL Lecture 4 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Lecture #5 מבוא מורחב.
Functions + Overloading + Scope
Run-Time Environments Chapter 7
Functional Programming
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
CS 326 Programming Languages, Concepts and Implementation
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
Variables, Environments and Closures
Computing Square Roots
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Material in the textbook on pages
Haskell.
CMPT 201 Functions.
Chapter 10 Programming Fundamentals with JavaScript
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.
Variables, Environments and Closures
Lecture #5 מבוא מורחב.
Lecture 18 Infinite Streams and
Functional Programming
PPL Sequence Interface.
CS220 Programming Principles
Dynamic Scoping Lazy Evaluation
Scoping and Binding of Variables
CSE 341 Section 5 Winter 2018.
Iterative Constructs Review
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Lecture 2 מבוא מורחב.
Predefined Functions Revisited
Lecture 2 מבוא מורחב.
Changing Data: (Continued)
Principles of Programming Languages
Lecture 2 - Names & Functions
Presentation transcript:

PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Today: High-Order Procedures (or procedures that manipulate procedures) In functional programming (hence, in Racket) procedures have a first class status: Can be a value of a variable Pass procedures as arguments Return procedures as values Create them at runtime Can be included in data structures

Motivation Example: sum-integers ;Signature: sum-integers(a,b) ;Purpose: to compute the sum of integers in the interval [a,b]. ;Type: [Number*Number -> Number] ;Post-conditions: result = a + (a+1) + ... + b. ;Example: (sum-integers 1 5) should produce 15 (define sum-integers (lambda (a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))) מטרת הפונ' היא לסכום המס' בתחום [a,b]

Motivation Example: sum-cubes ;Signature: sum-cubes(a,b) ;Purpose: to compute the sum of cubic powers of ;integers in the interval [a,b]. ;Type: [Number*Number -> Number] ;Post-conditions: result = a^3 + (a+1)^3 + ... + b^3. ;Example: (sum-cubes 1 3) should produce 36 (define sum-cubes (lambda (a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))) (define cube (lambda (x) (* x x x)))

Motivation Example: pi-sum (define pi-sum (lambda (a b) (if (> a b) 0 (+ (/ 1 (* a (+ a 2))) (pi-sum (+ a 4) b)))))

Same Pattern (define <name> (lambda (a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b))))) יש לנו תבנית החוזרת על עצמה.

Abstraction ;Signature: sum(term,a,next,b) ;Purpose: to compute the sum of terms, defined by <term> ;in predefined gaps, defined by <next>, in the interval [a,b]. ;Type: [[Num -> Num] * Num * [Num -> Num] * Num -> Num] ;Post-conditions: result = (term a) + (term (next a)) + ... (term n), ;where n = (next (next ...(next a))) =< b, ;(next n) > b. ;Example: (sum identity 1 add1 3) should produce 6, ;where ’identity’ is (lambda (x) x) and add1 is (lambda (x) (+ 1 x)) (define sum (lambda (term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))))

Using the Abstracted Form (define sum-integers (λ (a b) (sum id a add1 b))) (define sum-cubes (sum cube a add1 b))) (define pi-sum (sum pi-term a pi-next b))) (define id (λ (x) x)) (define add1 (+ x 1))) (define pi-term (/ 1 (* x (+ x 2))))) (define pi-next (+ x 4)))

Advantages Discussion: are there any? Code reuse General interface Easier maintenance, understanding, debugging… General interface Expresses a well-defined concept (in this case: sum)

Another Example: Integral (define dx 0.005) (define add-dx (λ (x) (+ x dx))) (define integral (λ (f a b) (* (sum f (+ a (/ dx 2)) add-dx b) dx))) > (integral cube 0 1) 0.2499875

More Abstraction: Sequence Operations ;Signature: … ;Type: [[Number*Number -> Number]*Number*Number*Number -> Number] ;… ;Example: (sequence-operation * 1 3 5) is 60 ;Tests: (sequence-operation + 0 2 2) ==> 2 (define sequence-operation (λ (operation start a b) (if (> a b) start (operation a (sequence-operation operation (+ a 1) b))))) גם פעולת החיבור והאיבר הניטרלי הם עכשיו פרמטרים.

Example of Sequence Operations > (sequence-operation * 1 3 5) 60 > (sequence-operation + 0 2 7) 27 > (sequence-operation - 0 3 5) 4 > (sequence-operation expt 1 2 4) 2417851639229258349412352 > (expt 2 (expt 3 4))

Anonymous Procedures From Greek: ἀνωνυμία, anonymia, meaning "without a name" or "namelessness"

Anonymous Procedures λ forms evaluated during computation (no define) Useful in many cases. (define pi-sum (lambda (a b) (sum (lambda (x) (/ 1 (* x (+ x 2)))) a (lambda (x) (+ x 4)) b)))

Anonymous Procedures Disadvantage: careless use may cause the same λ to reevaluate: (define sum-squares-iter (lambda (n sum) (if (= n 0) sum (sum-squares-iter (- n 1) (+ sum ((lambda (x) (* x x)) n))))))

Scope and Binding In a λ form every parameter has Binding (declaration) Occurrence Scope: lexical scoping In nested λ, things are a little tricky. An occurrence without binding is called free define is also declaration. Its scope is universal.

Scope and Binding (lambda (f a b dx) (* (sum f (+ a (/ dx 2.0)) (lambda (x) (+ x dx)) b) dx)) המשתנים המוגדרים הם f a b dx. הסקופ שלהם הוא כל הלאמבדה. המופעים של + * \ הם מופעים חופשיים.

Local Variables Essential programming technique: mostly used for saving repeated computation. Can we use scoping to define local variables? Yes we can!

Local Variables Consider the function: It is useful to define 2 local variables: a = 1+xy b = 1-y

Local Variables

Local Variables (define f (lambda (x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)) )) A ו-B מתנהגים כמו משתנים מקומיים: הערך שלהם מחושב פעם אחת בלבד, והסקופ שלהם הוא מקומי. שימו לב כי אי אפשר להגדיר את f_helper בסביבה הגלובלית בגלל שהוא צריך להכיר את x ו-y.

Let (define f (lambda ( x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))) מאקרו, או סוכר סינטקטי, ההופך ל-lambda אנונימית.

Let (let ( (<var1> <exp1>) (<var2> <exp2>) ... (<varn> <expn>) ) <body> ) חוק ההערכה של ביטוי let נובע מביטוי ה-lambda אליו הוא מתורגם: כל אחד מביטויי ה-exp עובר הערכה כל אחד מהמשתנים ב-body של ה-let מוחלף בערך המתאים.

Let vs. Anonymous Lambda (define f (lambda ( x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))) (define f (lambda (x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)) ))

Notes about Let let provides variables (declaration and scope) <expi> are in outer scope. <body> is the scope. Let variables are the bindings.

From Midterm 2008 סמן את כל הבלוקים הלקסיקליים (scopes) בקטע הבא. מהו הערך המוחזר? (let ((x 2)) (let ( (x 3) (y x) ) ((lambda (x y +) (+ x y)) (- x y) x *)))