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

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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.
מבוא מורחב 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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Hand Evals in DrRacket Hand evaluation helps you learn how Racket reduces programs to values.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
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.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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!!!
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.
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
Example 1: counter (set! vs set-box!) 1 Imperative Programming: Mutable data and local state (define counter (let ((count 0)) (lambda () (set! count (+
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
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.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 10
Additional Scheme examples
Functional Programming
PPL Lecture 3 Slides by Yaron Gonen,
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
CS 326 Programming Languages, Concepts and Implementation
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
Dynamic Scoping Lazy Evaluation
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.
Abstraction and Repetition
Modern Programming Languages Lecture 20 Fakhar Lodhi
6.001 SICP Streams – the lazy way
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
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.
Lecture #6 section pages pages72-77
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Abstraction and Repetition
Presentation transcript:

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

Midterm 2010 (define foo (let((f (lambda(x ) (+ 1 x)))) (lambda(x) (f x)))) תרגמו את ביטוי ה -let לביטוי ה -anonymous lambda מהו מס ' ה -closure שיווצרו ?

Today: High-Order Procedures (continued) Closures as returned values Delayed computation 4

Closures as Returned Values Number (+ x y y) [Num -> Num] (lambda (x) (+ x y y)) [Num -> [Num -> Num]] (lambda (y) (lambda (x) (+ x y y))) [Num -> Num] ((lambda (y) (lambda (x) (+ x y y))) 2) Number (((lambda (y) (lambda (x) (+ x y y))) 2) 5)

Example: derive ;Signature: deriv(f) ;Type: [[Number -> Number] -> [Number -> Number]] ;Example: for f(x)=x^3, the derivative is the function 3x^2, ;whose value at x=5 is 75. ;Tests: ((deriv cube) 5) ==> ~75 (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))))

Example: derive (define dx 0.001) (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))) >deriv # >(deriv (lambda (x) (* x x x))) # >((deriv (lambda (x) (* x x x))) 5)

Compile Time vs. Runtime A basic questions when dealing with returned procedures More a question of ‘is it a procedure already? or do we need to evaluate this lambda expression?’ When a procedure depends on other procedures, it is preferred that these auxiliary closures are created at compile time

Compile Time vs Runtime (define sqr (lambda (x) (* x x))) (sqr 5) ((lambda (x) (* x x)) 5) Compile time Runtime

Example: n th deriv (define nth-deriv (lambda (f n) (lambda (x) (if (= n 0) (f x) ((nth-deriv (deriv f) (- n 1)) x))))) (define dx 0.001) (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))) reminder

(define nth-deriv (lambda (f n) (lambda (x) (if (= n 0) (f x) ((nth-deriv (deriv f) (- n 1)) x))))) (define nth-deriv (lambda (f n) (if (= n 0) f (nth-deriv (deriv f) (- n 1))))) >(define five-exp (lambda (x) (* x x x x x))) >(define fourth-deriv-of-five-exp (nth-deriv five-exp 4)) > fourth-deriv-of-five-exp # (define nth-deriv (lambda (f n) (if (= n 0) f (lambda (x) ((nth-deriv (deriv f) (- n 1)) x))))) (define nth-deriv (lambda (f n) (if (= n 0) f (deriv (nth-deriv f (- n 1))))))

Compile time vs Runtime We will always prefer compile-time, but sometimes its not possile (remember f_helper ?)

Delayed Computation With lambda we can delay the computation. We can abuse it to solve hard problems in an elegant way.

and and or special forms (and... ) (or... )

Example (if condition consequence alternative) ==> (or (and condition consequence) alternative)

Example > (define x 0) > (define y 6) > (or (and (zero? x) ) (/ y x)) But what about > (if (zero? x) #f #t) #f > (or (and (zero? x) #f) #t) #t

Example The fixed version: (if condition consequence alternative) ==> ((or (and condition (lambda () consequence)) (lambda () alternative))) Delayed!

Delayed Computation for obtaining Iterative Process New method for making iteration of recursion using high-order procedures

Recursive Factorial (define fact (lambda (n) (if (= n 0) 1 (* (fact (- n 1)) n))))

Iterative Factorial (define fact-iter (lambda (n prod) (if (= n 0) prod (fact-iter (- n 1) (* n prod)))))

Iterative Factorial with Delayed Computation (define fact$ (lambda (n cont) (if (= n 0) (cont 1) (fact$ (- n 1) (lambda (res) (cont (* n res)))))))

(fact$ 2 (lambda (x) x)) (fact$ 1 (lambda (res) ((lambda (x) x) (* 2 res)))) (fact$ 0 (lambda (res) ((lambda (res) ((lambda (x) x) (* 2 res))) (* 1 res)))) ((lambda (res) ((lambda (x) x) (* 2 res))) (* 1 res))) 1) ((lambda (res) ((lambda (x) x) (* 2 res))) 1) ((lambda (x) x) 2) 2

Polymorphic Procedure ; Signature: average-damp(f) ; Type: [[Number -> Number] -> [Number -> Number]] (define average-damp (lambda (f) (lambda (x) (average x (f x))))) ; Type: [Number*Number -> Number] (define average (lambda (x y) (/ (+ x y) 2)))

Polymorphic Procedure ; Type: ; [[T 1 -> T 2 ]* ; [T 1 * T 2 -> T 3 ] ; -> T 3 ] (define op-damp (lambda (f op) (lambda (x) (op x (f x))))) > (op-damp - +) # > ((op-damp - +) 3) 0 > (op-damp not (lambda (x y) (and x y))) # > ((op-damp not (lambda (x y) (and x y))) #t) #f

Polymorphic Procedures Procedures that can accept arguments and return values of varying types. Expressions that create these procedures are polymorphic expressions.

(Useful!!) Example ; Type: [T * List -> Boolean] (define member? (lambda (el l) (cond [(list? l) (or (member? el (car l)) (member? el (cdr l)))] [(eq? el l) #t] [else #f])))