Higher-Order Procedures

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

מבוא מורחב 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 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Testing Michael Ernst CSE 140 University of Washington.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Python Functions.
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.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Lecture #5 מבוא מורחב.
Chapter 9: Value-Returning Functions
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
6.001 Jeopardy.
Computing Square Roots
Testing UW CSE 160 Winter 2017.
Material in the textbook on pages
CS21b: Structure and Interpretation
Design by Contract Fall 2016 Version.
6.001 SICP Data abstractions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
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.
Functions, Procedures, and Abstraction
Testing UW CSE 160 Winter 2016.
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
This Lecture Substitution model
CS220 Programming Principles
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
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 Streams – the lazy way
Extended Introduction to Computer Science
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
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 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Material in the textbook Sections to 1.2.1
Lecture #6 section pages pages72-77
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
Peter Seibel Practical Common Lisp Peter Seibel
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Good programming practices
Functions, Procedures, and Abstraction
Reasoning with Types.
Lecture 6 - Recursion.
Presentation transcript:

Higher-Order Procedures Today’s topics Procedural abstractions Capturing patterns across procedures – Higher Order Procedures

Procedural abstraction Process of procedural abstraction Define formal parameters, capture pattern of computation as a process in body of procedure Give procedure a name Hide implementation details from user, who just invokes name to apply procedure procedure Output: type Input: type Details of contract for converting input to output

Procedural abstraction example: sqrt To find an approximation of square root of x: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x)))) (define good-enuf? (lambda (guess x) (< (abs (- (square guess) x)) 0.001))) (define improve (lambda (guess x) (average guess (/ x guess)))) (define average (lambda (a b) (/ (+ a b) 2))) (define sqrt (lambda (x) (try 1 x)))

The universe of procedures for sqrt try improve average Good-enuf? sqrt sqrt

sqrt - Block Structure x: number : number sqrt (define sqrt (lambda (x) (define good-enuf? (lambda (guess) (< (abs (- (square guess) x)) 0.001))) (define improve (average guess (/ x guess)))) (define try (if (good-enuf? guess) guess (try (improve guess))))) (try 1)) ) good-enuf? improve try sqrt x: number : number

Typecasting We are going to find that it is convenient to reason about procedures (and data structures) in terms of the number and kinds of arguments, and the kind of output they produce We call this typing of a procedure or data structure

Types – a motivation (+ 5 10) ==> 15 (+ "hi” 5) ;The object "hi", passed as the first argument to integer-add, is not the correct type Addition is not defined for strings

Types – simple data We want to collect a taxonomy of expression types: Number Integer Real Rational String Boolean Names (symbols) We will use this for notational purposes, to reason about our code. Scheme checks types of arguments for built-in procedures, but not for user-defined ones.

Types – procedures Because procedures operate on objects and return values, we can define their types as well. We will denote a procedures type by indicating the types of each of its arguments, and the type of the returned value, plus the symbol  to indicate that the arguments are mapped to the return value E.g. number  number specifies a procedure that takes a number as input, and returns a number as value

Types (+ 5 10) ==> 15 (+ "hi” 5) ;The object "hi", passed as the first argument to integer-add, is not the correct type Addition is not defined for strings The type of the integer-add procedure is number, number  number two arguments, both numbers result value of integer-add is a number

Type examples expression: evaluates to a value of type: 15 number "hi" string square number  number > number,number  boolean (> 5 4) ==> #t The type of a procedure is a contract: If the operands have the specified types, the procedure will result in a value of the specified type Otherwise, its behavior is undefined  maybe an error, maybe random behavior

Types, precisely A type describes a set of scheme values number  number describes the set: all procedures, whose result is a number, which require one argument that must be a number Every scheme value has a type Some values can be described by multiple types If so, choose the type which describes the largest set Special form keywords like define do not name values therefore special form keywords have no type

number, number, number number Your turn The following expressions evaluate to values of what type? (lambda (a b c) (if (> a 0) (+ b c) (- b c))) (lambda (p) (if p "hi" "bye")) (* 3.14 (* 2 5)) number, number, number number Boolean string number

Summary of types type: a set of values every value has a type procedure types (types which include ) indicate number of arguments required type of each argument type of result of the procedure Types: a mathematical theory for reasoning efficiently about programs useful for preventing certain common types of errors basis for many analysis and optimization algorithms

What is procedure abstraction? Capture a common pattern (* 2 2) (* 57 57) (* k k) Formal parameter for pattern Actual pattern (lambda (x) (* x x)) Give it a name (define square (lambda (x) (* x x))) Note the type: number  number

Other common patterns 1 + 2 + … + 100 1 + 4 + 9 + … + 1002 (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ 1 a) b)))) (define (sum-squares a b) (if (> a b) 0 (+ (square a) (sum-squares (+ 1 a) b)))) (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b))))

Let’s examine this new procedure (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? (num  num, num, ________, ___)  num (num  num, num, num  num, num)  num (num  num, num, __________, ___)  num (num  num, num, num  num, ___)  num (________, ___, __________, ___)  num _________________________________  ___ (num  num, ___, __________, ___)  num _________________________________  num (__________, ___, __________, ___)  num What type is the output? How many arguments? What type is each argument?

Higher order procedures A higher order procedure: takes a procedure as an argument or returns one as a value (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ 1 a) b)))) (define (sum term a next b) (if (> a b) (+ (term a)(sum term (next a) next b)))) (define (sum-integers1 a b) (sum ) (lambda (x) x) a (lambda (x) (+ x 1)) b)

Higher order procedures (define (sum-squares a b) (if (> a b) 0 (+ (square a) (sum-squares (+ 1 a) b)))) (define (sum term a next b) (if (> a b) (+ (term a)(sum term (next a) next b)))) (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b))

Higher order procedures (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (define (sum term a next b) (if (> a b) (+ (term a)(sum term (next a) next b)))) (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b))

Higher order procedures Takes a procedure as an argument or returns one as a value (define (sum-integers1 a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b)) (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b)) (define (add1 x) (+ x 1)) (define (sum-squares1 a b)(sum square a add1 b)) (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b)) (define (add2 x) (+ x 2)) (sum (lambda (x) (/ 1 (square x))) a add2 b))

Returning A Procedure As A Value (define (add1 x) (+ x 1)) (define (add2 x) (+ x 2)) (define incrementby (lambda (n) . . . )) (define add1 (incrementby 1)) (define add2 (incrementby 2)) . . . (define add37.5 (incrementby 37.5)) incrementby: #  (#  #)

Returning A Procedure As A Value (define incrementby (lambda(n)(lambda (x) (+ x n)))) (incrementby 2)  ( (lambda(n)(lambda (x) (+ x n))) 2) (lambda (x) (+ x 2)) (incrementby 2)  a procedure of one var (x) that increments x by 2 ((incrementby 3) 4)  ? ( (lambda(x)(+ x 3)) 4) 

Nano-Quiz/Lecture Problem (define incrementby (lambda(n)(lambda (x) (+ x n)))) (define f1 (incrementby 6))  ? (f1 4)  (define f2 (lambda (x)(incrementby 6)))  ? (f2 4)  ? ((f2 4) 6)  ?

Procedures as values: Derivatives Taking the derivative is a function: What is its type? D:(#  # )  (#  #)

Computing derivatives A good approximation: (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x epsilon)) (f x)) epsilon)) )) (number  number)  (number number)

Using “deriv” (define square (lambda (y) (* y y)) ) (define epsilon 0.001) ((deriv square) 5) (define deriv (lambda (f) (lambda (x) (/ (- (f (+ x epsilon)) (f x)) epsilon)) ))

Finding fixed points of functions Here’s a common way of finding fixed points Given a guess x1, let new guess by f(x1) Keep computing f of last guess, till close enough (define (close? u v) (< (abs (- u v)) 0.0001)) (define (fixed-point f i-guess) (define (try g) (if (close? (f g) g) (f g) (try (f g)))) (try i-guess))

Using fixed points (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1)  1.6180  or x = 1 + 1/x when x = (1 + )/2 (define (sqrt x) (fixed-point (lambda (y) (/ x y)) 1)) Unfortunately if we try (sqrt 2), this oscillates between 1, 2, 1, 2, (define (fixed-point f i-guess) (define (try g) (if (close? (f g) g) (f g) (try (f g)))) (try i-guess))

So damp out the oscillation (define (average-damp f) (lambda (x) (average x (f x)))) Check out the type: (number  number)  (number  number) that is, this takes a procedure as input, and returns a NEW procedure as output!!! ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55

… which gives us a clean version of sqrt (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Compare this to Heron’s algorithm (the one we saw earlier) – same process, but ideas intertwined with code (define (cbrt x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1))

Procedures as arguments: a more complex example (define compose (lambda (f g x) (f (g x)))) (compose square double 3) (square (double 3)) (square (* 3 2)) (square 6) (* 6 6) 36 What is the type of compose? Is it: (number  number), (number  number), number  number No! Nothing in compose requires a number

Compose works on other types too (define compose (lambda (f g x) (f (g x)))) (compose (lambda (p) (if p "hi" "bye")) (lambda (x) (> x 0)) -5 ) ==> "bye" boolean  string number  boolean number result: a string Will any call to compose work? (compose < square 5) wrong number of args to < <: number, number  boolean (compose square double "hi") wrong type of arg to double double: number  number

Type of compose (define compose (lambda (f g x) (f (g x)))) Use type variables. compose: (B  C), (A  B), A  C Meaning of type variables: All places where a given type variable appears must match when you fill in the actual operand types The constraints are: F and G must be functions of one argument the argument type of G matches the type of X the argument type of F matches the result type of G the result type of compose is the result type of F

Higher order procedures Procedures may be passed in as arguments Procedures may be returned as values Procedures may be used as parts of data structures Procedures are first class objects in Scheme!!