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.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

MATH 224 – Discrete Mathematics
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 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Six compound procedures and higher-order procedures.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Complexity Analysis (Part I)
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
Six compound procedures and higher-order procedures.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
PPL Lecture 3 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.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
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.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Haskell Introduction CSCE 314 Spring CSCE 314 – Programming Studio Historical Background 1930s: Alonzo Church develops the lambda calculus, a simple.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus You could use: total.
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 SICP Compilation Context: special purpose vs. universal machines
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Introduction to Functional Programming in Racket
CS21b: Structure and Interpretation
Higher-Order Procedures
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
Functional Programming
Procedural vs Functional Style
This Lecture Substitution model
CS220 Programming Principles
The Metacircular Evaluator
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 Environment model
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
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.
6.001 SICP Explicit-control evaluator
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Today’s topics Abstractions Procedural Data
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
This Lecture Substitution model
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Functions, Procedures, and Abstraction
Presentation transcript:

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 2) September 2008

Spring 2008Programming Development Techniques 2 Procedural Abstraction We have seen the use of procedures as abstractions. So far we have defined cases where the abstractions that are captured are essentially compound operations on numbers. What does that buy us? –Assign a name to a common pattern (e.g., cube) and then we can work with the abstraction instead of the individual operations. What more could we do? –What about the ability to capture higher-level “programming” patterns. –For this we need procedures are arguments/return values from procedures

Spring 2008Programming Development Techniques 3 The really big idea Procedures (function) should be treated as first- class objects In scheme procedures (functions) are data –can be passed to other procedures as arguments –can be created inside procedures –can be returned from procedures This notion provides big increase in abstractive power One thing that sets scheme apart from most other programming languages

Spring 2008Programming Development Techniques 4 Section 1.3 -Terminology Procedures that accept other procedures as input or return a procedure as output are higher-order procedures. The other procedures are first-order procedures. Scheme treats functions/procedures as first- class objects. They can be manipulated like any other object.

Spring 2008Programming Development Techniques 5 Book and Here… Book goes through showing several examples of the abstract pattern of summation, and then shows how you might want to abstract that into a procedure. CAUTION: I find some of the names that they use for their abstraction confusing – don’t let that bother you! It just makes reading the book a little more difficult. I am going to borrow an introduction from some old slides from Cal-Tech. I think you should be able to put the two together very nicely. At least, that’s my intention…

Spring 2008Programming Development Techniques 6 In mathematics… Not all operations take in (only) numbers +, -, *, /, expt, log, mod, … –take in numbers, return numbers but operations like , d/dx, integration –take in functions –return functions (or numbers)

Spring 2008Programming Development Techniques 7 Math: Functions as Arguments You’ve seen: a=f(0)+f(1)+f(2)+f(3)+f(4)+f(5)+f(6)

Spring 2008Programming Development Techniques 8 Math: Functions as Arguments  is a “function” –which takes in a function a lower bound (an integer) an upper bound (also an integer) –and returns a number We say that  is a “higher-order” function Can define higher-order fns in scheme

Spring 2008Programming Development Techniques 9 Transforming summation is the same as…

Spring 2008Programming Development Techniques 10 Summation in scheme ; takes a function a low value and a high value ; returns the sum of f(low)...f(high) by incrementing ; by 1 each time (define (sum f low high) (if (> low high) 0 (+ (f low) (sum f (+ low 1) high))))

Spring 2008Programming Development Techniques 11 Evaluating summation Evaluate: (sum square 2 4) ((lambda (f low high) …) square 2 4) substitute: – square for f – 2 for low, 4 for high

Spring 2008Programming Development Techniques 12 …continuing evaluation (if (> 2 4) 0 (+ (square 2) (sum square 3 4))) (square 2) … 4 (+ 4 (sum square 3 4)))

Spring 2008Programming Development Techniques 13 …continuing evaluation (+ 4 (sum square 3 4))) (+ 4 (if (> 3 4) 0 (+ (square 3) (sum square 4 4)))) (+ 4 (+ (square 3) (sum square 4 4)))) (+ 4 (+ 9 (sum square 4 4))))

Spring 2008Programming Development Techniques 14 …continuing evaluation (+ 4 (+ 9 (sum square 4 4)))) yadda yadda… (+ 4 (+ 9 (+ 16 (sum square 5 4)))) (+ 4 (+ 9 (+ 16 (if (> 5 4) 0 …) (+ 4 (+ 9 (+ 16 0))) … 29 (whew!) pop quiz: what kind of process? –linear recursive

Spring 2008Programming Development Techniques 15 Also valid… (sum (lambda (x) (* x x)) 2 4) –this is also a valid call –equivalent in this case –no need to give the function a name

Spring 2008Programming Development Techniques 16 Iterative version sum generates a recursive process iterative process would use less space –no pending operations Can we re-write to get an iterative version?

Spring 2008Programming Development Techniques 17 Iterative version ; takes a function a low value and a high value ; returns the sum of f(low)...f(high) by incrementing ; by 1 each time (define (isum f low high) (sum-iter f low high 0)) (define (sum-iter f low high result) (if (> low high) result (sum-iter f (+ low 1) high (+ (f low) result))))

Spring 2008Programming Development Techniques 18 Evaluating iterative version (isum square 2 4) (sum-iter square 2 4 0) (if (> 2 4) 0 (sum-iter square (+ 2 1) 4 (+ (square 2) 0))) (sum-iter square (+ 2 1) 4 (+ (square 2) 0)) (sum-iter square 3 4 (+ 4 0)) (sum-iter square 3 4 4)

Spring 2008Programming Development Techniques 19 eval iterative sum cont’d... (sum-iter square 3 4 4) (if (> 3 4) 4 (sum-iter square (+ 3 1) 4 (+ (square 3) 4))) (sum-iter square (+ 3 1) 4 (+ (square 3) 4)) (sum-iter square 4 4 (+ 9 4)) (sum-iter square )

Spring 2008Programming Development Techniques 20 eval iterative sum cont’d... (sum-iter square ) (if (> 4 4) 13 (sum-iter square (+ 4 1) 4 (+ (square 4) 13))) (sum-iter square (+ 4 1) 4 (+ (square 4) 13)) (sum-iter square 5 4 ( )) (sum-iter square )

Spring 2008Programming Development Techniques 21 eval iterative sum cont’d... (sum-iter square ) (if (> 5 4) 29 (sum-iter...)) 29 same result, no pending operations more space-efficient

Spring 2008Programming Development Techniques 22 recursive vs. iterative (define (sum f low high) (if (> low high) 0 (+ (f low) (sum f (+ low 1) high)))) (define (isum f low high) (define (sum-iter f low high result) (if (> low high) result (sum-iter f (+ low 1) high (+ (f low) result)))) (sum-iter f a b 0))

Spring 2008Programming Development Techniques 23 recursive vs. iterative recursive: –pending computations –when recursive calls return, still work to do iterative: –current state of computation stored in operands of internal procedure –when recursive calls return, no more work to do (“tail recursive”)

Spring 2008Programming Development Techniques 24 Historical interlude Reactions on first seeing “lambda”: –What the heck is this thing? –What the heck is it good for? –Where the heck does it come from? This represents the essence of a function – no need to give it a name. It comes from mathematics. Where ever you might use the name of a procedure – you could use a lambda expression and not bother to give the procedure a name.

Spring 2008Programming Development Techniques 25 Generalizing summation What if we don’t want to go up by 1? Supply another procedure –given current value, finds the next one ; takes a function, a low value, a function to generate the next ; value and the high value. Returns f(low)...f(high) by ; incrementing according to next each time (define (gsum f low next high) (if (> low high) 0 (+ (f low) (gsum f (next low) next high))))

Spring 2008Programming Development Techniques 26 stepping by 1, 2,... ; takes a number and increments it by 1 (define (step1 n) (+ n 1)) ; new definition of sum... (define (new-sum f low high) ; same as before (gsum f low step1 high)) ; takes a number and increments it by 2 (define (step2 n) (+ n 2)) ; new definition of a summation that goes up by 2 each time (define (sum2 f low high) (gsum f low step2 high))

Spring 2008Programming Development Techniques 27 stepping by 2 (sum square 2 4) = (sum2 square 2 4) = (sum2 (lambda (n) (* n n n)) 1 10) =

Spring 2008Programming Development Techniques 28 using lambda (define (step2 n) (+ n 2)) (define (sum2 f low high) (gsum f low step2 high)) Why not just write this as: (define (sum2 f low high) (gsum f low (lambda (n) (+ n 2)) high)) don’t need to name tiny one-shot functions

Spring 2008Programming Development Techniques 29 (ab)using lambda How about: –sum of n 4 for n = 1 to 100, stepping by 5? (gsum (lambda (n) (* n n n n)) 1 (lambda (n) (+ n 5)) 100) NOTE: the n’s in the lambdas are independent of each other

Spring 2008Programming Development Techniques 30 Big Ideas Procedures (functions) are data! We can abstract operations around functions as well as numbers Provides great power –expression, abstraction –high-level formulation of techniques We’ve only scratched the surface!

Spring 2008Programming Development Techniques 31 Procedures without names (lambda (... ) ) (define (square x) (* x x)) (define square (lambda (x) (* x x))) lambda = create-procedure

Spring 2008Programming Development Techniques 32 Procedures are first-class objects Can be the value of variables Can be passed as parameters Can be return values of functions Can be included in data structures

Spring 2008Programming Development Techniques 33 Another Use for Lambda Providing “local” variables (define (make-rat a b) (cons (/ a (gcd a b)) (/ b (gcd a b)))) (define (make-rat a b) ((lambda (div) (cons (/ a div) (/ b div))) (gcd a b)))

Spring 2008Programming Development Techniques 34 More local variables ((lambda (x y) (+ (* x x) (* y y))) 5 7) ((lambda (v1 v2...) ) val-for-v1 val-for-v2...)

Spring 2008Programming Development Techniques 35 The let special form... (let (( ) ( )...) ) Translates into… ((lambda (... ) )... )

Spring 2008Programming Development Techniques 36 Using let (define (f x y) (let ((z (+ x y))) (+ z (* z z))))

Spring 2008Programming Development Techniques 37 Taking the Abstraction 1 Step Further… we can also construct and return functions.

Spring 2008Programming Development Techniques 38 Math: Operators as return values The derivative operator –Takes in… A function –(from numbers to numbers) –Returns… Another function –(from numbers to numbers)

Spring 2008Programming Development Techniques 39 Math: Operators as return values The integration operator –Takes in… A function –from numbers to numbers, and A value of the function at some point –E.g. F(0) = 0 –Returns A function from numbers to numbers

Spring 2008Programming Development Techniques 40 Returning operators So operators can be return values, as well:

Spring 2008Programming Development Techniques 41 Further motivation Besides mathematical operations that inherently return operators… …it’s often nice, when designing programs, to have operations that help construct larger, more complex operations.

Spring 2008Programming Development Techniques 42 An example: Consider defining all these functions: (define add1 (lambda (x) (+ x 1)) (define add2 (lambda (x) (+ x 2)) (define add3 (lambda (x) (+ x 3)) (define add4 (lambda (x) (+ x 4)) (define add5 (lambda (x) (+ x 5)) …repetitive, tedious.

Spring 2008Programming Development Techniques 43 Avoid Needless Repetition (define add1 (lambda (x) (+ x 1)) (define add2 (lambda (x) (+ x 2)) (define add3 (lambda (x) (+ x 3)) (define add4 (lambda (x) (+ x 4)) (define add5 (lambda (x) (+ x 5)) –Whenever we find ourselves doing something rote/repetitive… ask: Is there a way to abstract this?

Spring 2008Programming Development Techniques 44 Abstract “Up” Generalize to a function that can create adders: ; function that takes a number and returns a function ; that takes a number and adds that number to the given number (define (make-addn n) (lambda (x) (+ x n))) ;(define make-addn ;; equivalent def ; (lambda (n) ; (lambda (x) (+ x n))))

Spring 2008Programming Development Techniques 45 How do I use it? (define (make-addn n) (lambda (x) (+ x n))) ((make-addn 1) 3) 4 (define add3 (make-addn 3)) (define add2 (make-addn 2)) (add3 4) 7

Spring 2008Programming Development Techniques 46 Evaluating… (define add3 (make-addn 3)) –Evaluate (make-addn 3) Evaluate 3 -> 3. Evaluate make-addn -> –(lambda (n) (lambda (x) (+ x n))) Apply make-addn to 3… –Substitute 3 for n in (lambda (x) (+ x n)) –Get (lambda (x) (+ x 3)) –Make association: add3 bound to (lambda (x) (+ x 3))

Spring 2008Programming Development Techniques 47 Evaluating (add3 4) (add3 4) Evaluate 4 Evaluate add3  (lambda (x) (+ x 3)) Apply (lambda (x) (+ x 3)) to 4  Substitute 4 for x in (+ x 3)  (+ 4 3) 77

Spring 2008Programming Development Techniques 48 Big Ideas We can abstract operations around functions as well as numbers We can “compute” functions just as we can compute numbers and booleans Provides great power to –express –abstract –formulate high-level techniques