day06

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

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

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 6: October 15, 2008 Higher Order Procedures
Background image of page 1

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

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 Star Wars (1977)
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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)
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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)
Background image of page 7

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

View Full Document Right Arrow Icon
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)
Background image of page 8
Caltech CS 1 -- Fall 2008 Base case (define (sum f low high) (if (> low high) ; base case 0 ; nothing to sum ???)) ; how to decompose?
Background image of page 9

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

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 Decomposing summation is the same as…
Background image of page 10
Caltech CS 1 -- Fall 2008 Summation in Scheme (define (sum f low high) (if (> low high) 0 (+ (f low) (sum f (+ low 1) high))))
Background image of page 11

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

View Full Document Right Arrow Icon
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
Background image of page 12
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
Background image of page 13

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

View Full Document Right Arrow Icon
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)))
Background image of page 14
…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
Background image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
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 Right Arrow Icon
Ask a homework question - tutors are online