day06

# day06 - CS1 Introduction to Computation Day 6 Higher Order...

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

Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 6: October 15, 2008 Higher Order Procedures

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

View Full Document
Caltech CS 1 -- Fall 2008 Star Wars (1977)
Caltech CS 1 -- Fall 2008 Today • A really, REALLY big idea: – procedures (functions) are data – can be passed to other procedures as arguments – can be created inside procedures – can be returned from procedures • Provides big increase in abstractive power • This feature not found in most computer languages

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

View Full Document
Caltech CS 1 -- Fall 2008 In mathematics… • Not all operations take in (only) numbers • + , - , * , / , expt , log , mod , … – take in numbers, return numbers but operations like Σ , d/dx , integration ( ) – take in functions – return numbers or functions
Caltech CS 1 -- Fall 2008 Math: Functions as Arguments • You’ve seen: a=f(0)+f(1)+f(2)+f(3)+f(4)+f(5)+f(6)

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

View Full Document
Caltech CS 1 -- Fall 2008 Math: Functions as Arguments Σ is a “function” – which takes in • a function • a lower bound (an integer) • an upper bound (also an integer) – and returns • a number We say that Σ is a “higher-order” function • Can define higher-order funcs in Scheme
Caltech CS 1 -- Fall 2008 Recall: design strategy • Write the contract and comment • Then figure out base cases • Then figure out how to decompose problem in terms of itself • Then write tests – (but we'll skip that here)

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

View Full Document
Caltech CS 1 -- Fall 2008 Contract etc. ;; sum: (number -> number) number number -> ;; number ;; This function computes the sum of the ;; function f between the limits low and high. (define (sum f low high) ...) Note first argument in the contract specifies a function type (number -> number)
Caltech CS 1 -- Fall 2008 Base case (define (sum f low high) (if (> low high) ; base case 0 ; nothing to sum ???)) ; how to decompose?

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

View Full Document
Caltech CS 1 -- Fall 2008 Decomposing summation is the same as…
Caltech CS 1 -- Fall 2008 Summation in Scheme (define (sum f low high) (if (> low high) 0 (+ (f low) (sum f (+ low 1) high))))

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

View Full Document
Caltech CS 1 -- Fall 2008 Evaluating summation [We'll skip some steps for brevity] • Evaluate: (sum square 2 4) • ((lambda (f low high) …) square 2 4) • Substitute: square for f 2 for low , 4 for high
Caltech CS 1 -- Fall 2008 …continuing evaluation • (if (> 2 4) 0 (+ ( square 2) (sum square 3 4))) • (+ (square 2) (sum square 3 4)) • (square 2) 4 • (+ 4 (sum square 3 4)) subst into operator position

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

View Full Document
Caltech CS 1 -- Fall 2008 …continuing evaluation • (+ 4 (sum square 3 4)) • (+ 4 (if (> 3 4) 0 (+ (square 3) (sum square 4 4)))) • (+ 4 (+ (square 3) (sum square 4 4))) • (+ 4 (+ 9 (sum square 4 4)))
…continuing evaluation • (+ 4 (+ 9 (sum square 4 4))) • yadda yadda… • (+ 4 (+ 9 (+ 16 (sum square 5 4)))) • (+ 4 (+ 9 (+ 16 (if (> 5 4) 0 …) • (+ 4 (+ 9 (+ 16 0))) • … 29 • Pop quiz: what kind of process? – linear recursive

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 76

day06 - CS1 Introduction to Computation Day 6 Higher Order...

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

View Full Document
Ask a homework question - tutors are online