+ All Categories
Home > Documents > Higher Order Procedures

Higher Order Procedures

Date post: 27-Nov-2023
Category:
Upload: independent
View: 0 times
Download: 0 times
Share this document with a friend
44
CSE221 Structure and Interpretation of Computer Programs Higher-Order Procedures
Transcript

CSE221Structure and Interpretation of

Computer Programs

Higher-Order Procedures

Sum of Integers from a through b

a = 1b = 5

sum-integers = 1 + 2 + 3 + 4 + 5

Sum of Integers from a through b

(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

Sum of Cubes from a through b

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

Sum of Sequence of Termsin Series

11⋅3

+ 15⋅7

+ 19⋅11

+⋯

Sum of Sequence of Termsin Series

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Abstracting Common Structure

(define (<name> a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

sum

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

term

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

next

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

next

sum

term

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Procedures as Arguments

(define ( a b) (if (> a b) 0 (+ ( ____ a) ( ______ (_____ a) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

sum

Abstracting Common Structure

(define (<name> a b) (if (> a b) 0 (+ (<term> a) (<name> (<next> a) b))))

Procedures as Arguments

(define (sum a b) (if (> a b) 0 (+ ( ____ a) (sum (_____ a) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

term

Procedures as Arguments

(define (sum term a b) (if (> a b) 0 (+ (term a) (sum term (_____ a) b))))

Abstracting Common Structure(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

next

Procedures as Arguments

(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))

Sum of Integers from a through b

(define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) b))))

Sum-Integers(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b))))

(define (identity x) x)

(define (sum-integers a b) (sum identity a inc b))

(define (inc n) (+ n 1))

Sum of Cubes from a through b

(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))

Sum-Cubes(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b))))

(define (cube x) (* x x x))

(define (sum-cubes a b) (sum cube a inc b))

(define (inc n) (+ n 1))

Sum of Sequence of Termsin Series

(define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))

Pi-Sum(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b))))

(define (pi-term x) (/ 1.0 (* x (+ x 2))))

(define (pi-sum a b) (sum pi-term a pi-next b))

(define (pi-next x) (+ x 4))

(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b))))

(define (sum-integers a b) (sum identity a inc b))

(define (sum-cubes a b) (sum cube a inc b))(define (pi-sum a b)

(sum pi-term a pi-next b))

Abstracting Common Structure

Example of Re-use

∫a

b

f =[ f (a+ dx2

)+ f (a+dx+ dx2

)+ f (a+2dx+ dx2

)+⋯]dx

(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b))))

Example of Re-use

(define (integral f a b dx) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))

∫a

b

f =[ f (a+ dx2

)+ f (a+dx+ dx2

)+ f (a+2dx+ dx2

)+⋯]dx

(define (add-dx x) (+ x dx))

Example of Re-use

(define (integral f a b dx) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx))

∫a

b

f =[ f (a+ dx2

)+ f (a+dx+ dx2

)+ f (a+2dx+ dx2

)+⋯]dx

(define (add-dx x) (+ x dx))

(integral cube 0 1 0.01)

Define a procedure compose thatimplements the following

composition

((compose square inc) 6)49

(define (compose f g) (lambda (x) (f (g x))))

Define a procedure double thattakes a procedure of one argument

as argument and returns a procedure that applies the original

Procedure twice

(define (double f) (lambda (x) (f (f x))))

What value is returned by

((double (double (double inc))) 5)

What value is returned by

(((double (double double)) inc) 5)


Recommended