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

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

Introduction to Recursion and Recursive Algorithms
(define (f x) (if (< x 0) (lambda (y) (- y x)) (lambda (y) (- x y)))) GE f: P1 para:x body:(if … )
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.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
6.001 SICP – September Introduction
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Discrete Mathematics Recursion and Sequences
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
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.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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!!!
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
PPL Lecture 3 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
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))))))
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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.
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.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
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.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
CSC 205 Programming II Lecture 8 Recursion.
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
PPL Lecture 3 Slides by Yaron Gonen,
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Env. Model Implementation
Higher-Order Procedures
This Lecture Substitution model
CS220 Programming Principles
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.
6.001 SICP Further Variations on a Scheme
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
This Lecture Substitution model
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) Procedures as returned values Delayed computation 4

Procedures 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 dx) ;Type: [[Number -> 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 0.001) 5) ==> ~75 (define deriv (lambda (f dx) (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 More a question of ‘is it a closure already? Or do we need to evaluate this lambda expression?’ When a closure depends on other closures, 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 (lambda (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 nth-deriv (lambda (f n) (if (= n 0) f (deriv (nth-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 #

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 Computation for obtaining Iterative Process Reminder: recursion stores future computation 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