{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-functional-part3

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

This preview shows pages 1–7. Sign up to view the full content.

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)

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

View Full Document
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 ) (
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

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

View Full Document
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 * ) ) ( ( ) ( , ! " + = #
5 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 x f x F ) ( ) (

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

View Full Document
6 Further motivation Besides mathematical operations that inherently return operators, it’s nice to have operations that help construct larger, more complex operations Example: (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 and tedious Is there a way to abstract this?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}