05-functional-part3 - Functional Programming Part 3 Outline...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Functional Programming Part 3 Outline Previous Lecture Linear recursive process Linear iterative process Tree recursive process More on Scheme Higher Order Functions Global and local variables Compound data types Data Abstraction Higher Order Functions Recall that a higher-order function either takes operators as parameters, yields an operator as its result, or both Why do we need higher-order function? In mathematics, not all operators deal exclusively with numbers +, -, *, /, expt, log, mod, … Take in numbers, return numbers But, mathematical operations like Σ , d/dx Take in operators Return operators (or numbers)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Operators as Parameters (define (sum f low high) (if (> low high) 0 (+ ( f low) (sum f (+ low 1) high)))) ! = = 5 0 2 x x a Operators as Parameters (sum (lambda (x) (* x x)) 0 5) (+ ( (lambda (x) (* x x)) 0) (sum (lambda (x) (* x x)) 1 5)) (+ 0 (+ ( (lambda (x) (* x x)) 1) (sum (lambda (x) (* x x)) 2 5))) …. (+ 0 (+ 1 (+ 4 (+ 9 (+ 16 (+ 25 0)))))) ! = = 5 0 2 x x a Generalized summation What if we don’t want to go up by 1? Supply another procedure given current value, finds the next one (define (gsum f low next high) (if (> low high) 0 (+ (f low) (gsum f ( next low) next high)))) ! = high low x x f ) (
Background image of page 2
3 stepping by 1, 2, . .. (define (step1 n) (+ n 1)) (define (sum f low high) (gsum f low step1 high)) (define (step2 n) (+ n 2)) (define (sum2 f low high) (gsum f low step2 high)) stepping by 2 (define (square n) (* n n)) (sum square 2 4) = 2 2 + 3 2 + 4 2 (sum2 square 2 4) = 2 2 + 4 2 (sum2 (lambda (n) (* n n n)) 1 10) = 1 3 + 3 3 + 5 3 + 7 3 + 9 3 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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 One last function Definite integral of f(x) from a to b If this was a sum, we could do it. .. ! b a dx x f ) ( approximate as a sum a b dx f(a) f(a+dx) dx x f dx x f b a b dx a x * ) ) ( ( ) ( , ! " + = #
Background image of page 4
Integration in scheme. .. dx x f dx x f b a b dx a x * ) ) ( ( ) ( , ! " + = # (define (integral f a b dx) (* dx (gsum f a (lambda (x) (+ x dx)) b))) Example (integral (lambda (x) (* x x)) 0 1 0.0001) => 0.3333833349999416 ! = 1 0 2 3 / 1 dx x Operators as return values The derivative operator Takes in… A function Returns… Another function 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 )) ( ( ) ( x F dx d x f = ! = dx
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 452 taught by Professor Cheng during the Fall '05 term at Michigan State University.

Page1 / 22

05-functional-part3 - Functional Programming Part 3 Outline...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online