Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.

Slides:



Advertisements
Similar presentations
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
Advertisements

PPL Lecture 3 Slides by Dr. Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. 2 Outline Let High order procedures.
1 Gentle Introduction to Programming Session 3: Higher Order Functions, Recursion.
6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page:
1 Gentle Introduction to Programming Session 2: Functions.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
מבוא מורחב - שיעור 6 1 Lecture 6 High order procedures Primality testing The RSA cryptosystem.
Numerical Methods Solution of Equation.
Suppose we are given a differential equation and initial condition: Then we can approximate the solution to the differential equation by its linearization.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Real Zeros of Polynomial Functions
1 M 277 (60 h) Mathematics for Computer Sciences Bibliography  Discrete Mathematics and its applications, Kenneth H. Rosen  Numerical Analysis, Richard.
Chapter 5 Numerical Root Findings
Lecture #5 מבוא מורחב.
Numerical Methods Some example applications in C++
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
High order procedures Primality testing The RSA cryptosystem
Chapter 6 Section 4.
Theory of Computation Lecture 4: Programs and Computable Functions II
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Computing Square Roots
Material in the textbook on pages
CIE Centre A-level Pure Maths
6.001 SICP Data abstractions
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 מבוא מורחב.
Lecture 18 Infinite Streams and
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.
Intermediate Value Theorem
6.001 SICP Streams – the lazy way
Extended Introduction to Computer Science
Intermediate Value Theorem
Notes Over 9.1 Finding Square Roots of Numbers
4 Numerical Methods Root Finding.
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
Lecture #6 section pages pages72-77
Newton’s Method and Its Extensions
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
The Fundamental Theorems of Calculus
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Rules of evaluation The value of a number is itself.
Good programming practices
1 Newton’s Method.
Generating Random Variates
Presentation transcript:

Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5

Types so far Numbers: 1, 7, 1.2 Boolean: #t , #f Strings: “this is a string” Procedures: + (lambda (x) (if (< x 0) “x is negative” “x is not negative”)) מבוא מורחב - שיעור 5

Procedures have types A procedure may have requirements regarding the number of its arguments, may expect each argument to be of a certain type. The procedure + expects numbers as its arguments. Cannot be applied to strings. (+ “abc” “xyz”) מבוא מורחב - שיעור 5

Procedures have types The type of a procedure is (part of) 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 A contract between the caller and the procedure. caller responsible for argument number and types procedure responsible to deliver correct result מבוא מורחב - שיעור 5

number × number  number Example The type of the procedure add: (define (add x y) (+ x y)) is: two arguments, both numbers result value of number addition is a number number × number  number (+ 7 “xx”) - causes an error. מבוא מורחב - שיעור 5

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))) number × number × number number (lambda (p) (if p "hi" "bye")) Boolean string (* 3.14 (* 2 5)) number (lambda (p) (if p "hi" 0)) ?? מבוא מורחב - שיעור 5

Types (summary) type: a set of possible values and operations 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 מבוא מורחב - שיעור 5

Can procedures get and return procedures? In scheme a procedure can: Return a procedure as its return value, Receive procedures as arguments. Why is this useful? מבוא מורחב - שיעור 5

Consider the following three sums 1 + 2 + … + 100 = (100 * 101)/2 1 + 4 + 9 + … + 1002 = (100 * 101 * 102)/6 1 + 1/32 + 1/52 + … + 1/1012 ~ p2/8 In mathematics they are all captured by the notion of a sum: מבוא מורחב - שיעור 5

Let’s have a look at the three programs (define (pi-sum a b) (if (> a b) 0 (+ (/ 1 (square a)) (pi-sum (+ a 2) b)))) (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 (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) מבוא מורחב - שיעור 5

Let’s check this new procedure out! (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) What is the type of this procedure? ((number  number) × number × (number number) × number)  number procedure procedure procedure מבוא מורחב - שיעור 5

Higher-order procedures (define (sum term a next b) (if (> a b) (+ (term a) (sum term (next a) next b)))) Examples of use: 1. (define (sum-integers1 a b) (sum (lambda (x) x) a (lambda (x) (+ x 1)) b)) 2. (define (sum-squares1 a b) (sum square a (lambda (x) (+ x 1)) b)) 3. (define (pi-sum1 a b) (sum (lambda (x) (/ 1 (square x))) a (lambda (x) (+ x 2)) b)) A higher-order procedure: takes one or more procedures as arguments and/or returns one as a value מבוא מורחב - שיעור 5

… How does it work? (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (sum square 1 (lambda (x) (+ x 1)) 100) (+ (square 1) (sum square ((lambda (x) (+ x 1)) 1) (lambda (x) (+ x 1)) 100)) (+ 1 (sum square 2 (lambda (x) (+ x 1)) 100)) (+ 1 (+ (square 2) (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (sum square 3 (lambda (x) (+ x 1)) 100))) (+ 1 (+ 4 (+ 9 (sum square 4 (lambda (x) (+ x 1)) 100))) … מבוא מורחב - שיעור 5

Integration as a procedure Integration under a curve f is approximated by dx (f(a) + f(a + dx) + f(a + 2dx) + … + f(b)) a b dx f (define (integral f a b) (define dx 1.0e-3) (* (sum f a (lambda (x) (+ x dx)) b) dx)) arctan(a) = ∫(1/(1+y2))dy a (define atan (lambda (a) (integral (lambda (y) (/ 1 (+ 1 (square y)))) 0 a))) מבוא מורחב - שיעור 5

The derivative. We want to write a procedure with: Argument: a function f: number  number Result: the function f’: number  number deriv: (number  number)  (number  number) (define (deriv f) (lambda (x) (define dx 0.001) (/ (- (f (+ x dx)) (f x)) dx))) > ((deriv square) 3) 6.000999999999479 מבוא מורחב - שיעור 5

We will soon see more examples of higher-order procedures Before that, LET us have an intermezzo מבוא מורחב - שיעור 5

The syntactic sugar “Let” Suppose we wish to implement the function f(x,y) = x(1+x*y)2 + y(1-y) + (1+x*y)(1-y) We can also express this as a = 1+x*y b = 1-y f(x,y) = xa2 + yb + ab a b מבוא מורחב - שיעור 5

The syntactic sugar “Let” (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y))) (define (f x y) ((lambda (a b) (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))) מבוא מורחב - שיעור 5

The syntactic sugar “Let” (Let ((<var1> <exp1>) (<var2> <exp2>) .. (<varn> <expn>)) <body>) bindings Is defined to be equivalent to: ((lambda (<var1> ….. <varn>) <body>) <exp1> <exp2> … <expn>) מבוא מורחב - שיעור 5

More Procedural Abstraction… Fixed Points x0 is a fixed point of F(x) if F(x0) = x0 x0 = Example: a is a fixed point of F(x) = a/x

Finding fixed points for f(x) Start with an arbitrary first guess x1 Each time: try the guess, f(x) ~ x ?? If it’s not a good guess try the next guess xi+1 = f(xi) (define (fixed-point f first-guess) (define tolerance 0.00001) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) guess (try next)))) (try first-guess)) מבוא מורחב - שיעור 5

An example: f(x) = 1+1/x (define (f x) (+ 1 (/ 1 x))) (fixed-point f 1.0) X1 = 1.0 X2 = f(x1) = 2 X3 = f(x2) = 1.5 X4 = f(x3) = 1.666666666.. X5 = f(x4) = 1.6 X6 = f(x5) = 1.625 X7 = f(x6) = 1.6153846… Exact fixed-point: 1.6180339… Note how odd guesses underestimate And even guesses Overestimate. מבוא מורחב - שיעור 5

Another example: f(x) = 2/x (define (f x) (/ 2 x)) (fixed-point f 1.0) x1 = 1.0 x2 = f(x1) = 2 x3 = f(x2) = 1 x4 = f(x3) = 2 x5 = f(x4) = 1 x6 = f(x5) = 2 x7 = f(x6) = 1 Exact fixed-point: 1.414213562… מבוא מורחב - שיעור 5

How do we deal with oscillation? Consider f(x)=2/x. If guess is a number such that guess < sqrt(2) then 2/guess > sqrt(2) So the average of guess and 2/guess is always an even Better guess. So, we will try to find a fixed point of g(x)= (x + f(x))/2 Notice that g(x) = (x +f(x)) /2 has the same fixed points as f. For f(x)=2/x this gives: g(x)= (x + 2/x)/2 מבוא מורחב - שיעור 5

To find an approximation of x: Make a guess G Improve the guess by averaging G and x/G Keep improving the guess until it is good enough X = 2 G = 1 X/G = 2 G = ½ (1+ 2) = 1.5 X/G = 4/3 G = ½ (3/2 + 4/3) = 17/12 = 1.416666 X/G = 24/17 G = ½ (17/12 + 24/17) = 577/408 = 1.4142156 מבוא מורחב - שיעור 5

Extracting the common pattern: average-damp (define (average-damp f) ;outputs g(x)=(x+f(x))/2 (lambda (x) (average x (f x)))) average-damp: (number  number)  (number  number) ((average-damp square) 10) ((lambda (x) (average x (square x))) 10) (average 10 (square 10)) 55 מבוא מורחב - שיעור 5

… which gives us a clean version of sqrt (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Compare this to our previous implementation of sqrt – same process. For the cubic root of x, fixed point of f(y) = x/y2 (define (cubert x) (average-damp (lambda (y) (/ x (square y)))) מבוא מורחב - שיעור 5

Further abstraction (define (osc-fixed-point f first-guess) (fixed-point (average-damp f) first-guess)) (define (sqrt x) (osc-fixed-point (lambda (y) (/ x y)) 1.0) (define (cubert x) (osc-fixed-point (lambda (y) (/ x (square y))) מבוא מורחב - שיעור 5

A solution to the equation: F(x) = 0 Newton’s method A solution to the equation: F(x) = 0 is a fixed point of: G(x) = x - F(x)/F’(x) (define (newton-transform f) (lambda (x) (- x (/ (f x) ((deriv f) x))))) (define (newton-method f guess) (fixed-point (newton-transform f) guess)) (define (sqrt x) (newton-method (lambda (y) (- (square y) x)) 1.0)) מבוא מורחב - שיעור 5

Further abstraction (define (fixed-point-of-transform f transform guess) (fixed-point (transform f) guess)) (define (osc-fixed-point f guess) (fixed-point-of-transform f average-damp guess)) (define (newton-method f guess) newton-transform מבוא מורחב - שיעור 5