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

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
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.
מבוא מורחב 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 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Reading and Writing Mathematical Proofs
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Higher-Order Procedures (in Ruby) based on ‘Structure and Interpretation of Computer Programs’ (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
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,
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Going Functional Primož Gabrijelčič. Functional programming.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
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.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
9-October-2002cse MoreLambda © 2002 University of Washington1 More Lambda CSE 413, Autumn 2002 Programming Languages
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
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 מבוא מורחב.
Functional Programming
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
PPL Lecture 3 Slides by Yaron Gonen,
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
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.
CMPT 201 Functions.
Programming Language Concepts
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CS21b: Structure and Interpretation
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 מבוא מורחב.
PPL Sequence Interface.
CS220 Programming Principles
Dynamic Scoping Lazy Evaluation
Scoping and Binding of Variables
CSE 341 Section 5 Winter 2018.
Material in the textbook Sections to 1.2.1
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
The structure of programming
Lecture 2 מבוא מורחב.
CSE 341 Lecture 11 b closures; scoping rules
Lecture 2 מבוא מורחב.
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

Warm-up Is this recursive or iterative? (define sum (lambda (n) (if (= n 0) 0 (+ (/ 1 (pow 2 n)) (sum (- n 1))))))

The Iterative Version (define sum-iter (lambda (n prod) (if (= n 0) prod (sum-iter (- n 1) (+ prod (/ 1 (pow 2 n)))))))

Today: High-Order Procedures In functional programming (hence, in Scheme) procedures have a first class status: Pass procedures as arguments Create them at runtime Help in creating local-variables 5

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)))))

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)^ 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)))))

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 (lambda (a b) (if (> a b) 0 (+ ( a) ( ( a) b)))))

Abstraction ;Signature: sum(term,a,next,b) ;Purpose: to compute the sum of terms, defined by ;in predefined gaps, defined by, 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) (define sum (lambda (term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))))

Using the abstracted form (define id (lambda(x) x)) (define add1 (lambda (x) (+ x 1))) (define pi-term (lambda (x) (/ 1 (* x (+ x 2))))) (define pi-next (lambda (x) (+ x 4))) (define sum-integers (lambda (a b) (sum id a add1 b))) (define pi-sum (lambda (a b) (sum pi-term a pi-next b)))

Advantages Code reuse – Easier maintenance, understanding, debugging… General interface – Expresses a well-defined concept – Allows for further abstraction (next slide)

Sequence Operations ;Signature: … ;Type: [[Number*Number -> Number]*Number*Number*Number -> Number] ;… ;Example: (sequence-operation * 1 3 5) is 60 ;Tests: (sequence-operation ) ==> 2 (define sequence-operation (lambda (operation start a b) (if (> a b) start (operation a (sequence-operation operation start (+ a 1) b)))))

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))

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

(define f (lambda (x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y)) ))

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

Let (let ( ( ) ( )... ( ) ) )

Let vs. Anonymous Lambda Let (define f (lambda ( x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b))))) Lambda (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) are in outer scope. is the scope. Let variables are the binind.

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