100 likes | 113 Views
This recitation session provides an overview of procedures as first-class citizens in the context of "Structure and Interpretation of Computer Programs". Topics covered include anonymous procedures, anonymous numbers, lambda expressions, and higher-order procedures.
E N D
6001structure & interpretation of computer programsrecitation 5/ september 19, 1997
overview • today’s big idea • procedures as first-class citizens
anonymous procedures • anonymous numbers • the expression (* (+ 2 3) 4)) contains the subexpression (+ 2 3) • we could introduced a name for the subexpression’s value instead • with (define i (+ 2 3)) and then written (* i 4) • but it’s convenient to have anonymous values • anonymous procedures • compare this to • (define (square i) (* i i)) • (square 5) • what if we want to make the squaring procedure anonymous? • we can write an expression whose value is the squaring procedure like this: • (lambda (i) (* i i)) • now for (square 5), we can write • ((lambda (i) (* i i)) 5) • is this a big deal? • we’ll see more compelling uses of lambda later • lambda will allow us to construct a procedure on the fly
define revisited • recall two versions of define • for simple values • (define i 3) • for procedures • (define (square i) (* i i)) • now that we’ve seen lambda • only the first form is significant • the second form is syntactic sugar • (define (square i) (* i i)) • stands for • (define square (lambda (i) (* i i))
simple examples of higher-order procedures • procedure as argument • (define (apply-either p q x) ((if (> x 0) p q) x)) • (define (abs x) (apply-either + - x)) • (define (abs-again x) (apply-either (lambda (i) i) - x)) • procedure as result (1) • (define (op b) (if b * +)) • ((op #t) 4 5) –> 9 • procedure as result (2) • (define (power k) (lambda (x) (exp x k)) • (define cube (power 3)) • note • both op and power return procedure values • op selects its value from a finite, fixed repertoire:all the possible return values appear syntactically in its body • power constructs a procedure on the fly:its repertoire of results is unbounded • languages like C and Pascal allow you to write procedures like op, but not like power
a more realistic problem • calculating compound interest • procedure to calculate compound interest • (define (compound principal rate term) • (define (citer year gross) • (if (= term year) gross (citer (inc year) (+ gross (* gross rate))))) • (citer 0 principal)) • sample evaluations • (compound 1000 0.05 20) ; evals to 2653 • (compound 1000 0.05 40) ; evals to 7040 – so invest early! • (compound 1000 0.10 40) ; evals to 45259 – so get good rate!
procedures as arguments • problem • allow the rate to vary as function of year • solution • use a procedure parameter (ratep) • (define (compound-vary principal ratep term) • (define (citer year gross) • (if (= term year) gross (citer (inc year) (+ gross (* gross (ratep year)))))) • (citer 0 principal)) • sample evaluations • (define (five year) 0.05) ; fixed rate of 5% • (compound-vary 1000 five 20) ; evals to 2653, as before • (define (up year) ; rate ramps up (+ 0.05 (/(modulo year 10) 100))) • (compound-vary 1000 up 20) ; evals to 6099 • (define (dn year) ; rate ramps down (+ 0.15 (- (/(modulo year 10) 100)))) • (compound-vary 1000 dn 20) ; evals to 7316
procedures as arguments, ctd • problem • allow new gross to be arbitrary function of old gross and year • solution • use procedure parameter that takes two arguments • (define (compound-flex principal app term) • (define (citer year gross) • (if (= term year) gross (citer (inc year) (app gross year)))))) • (citer 0 principal)) • sample argument • (define (phased g y)(define rate (cond ((> g 1000) 0.10)) ((> g 500) 0.05) (else 0.02))))(* g (+ 1 rate))) • sample applications • (compound-flex 1000 phased 20) ; evals to 6421 • (compound-flex 100 phased 20) ; evals to 148
procedures as results • problem • write a procedure make-fixed-app such that(make-fixed-app p) gives a procedure that can be supplied as the appreciator argument to compound-f, and which gives a fixed rate of p percent • solution • define a procedure that returns a procedure • (define (make-fixed-app percent) • (lambda (gross year) (+ gross (* 0.01 percent gross)))) • sample evaluations • (define five-per-year (make-fixed-app 5)) • (define ten-per-year (make-fixed-app 10)) • (compound-flex 1000 five-per-year 20) ; evaluates to 2653 • (compound-flex 1000 ten-per-year 20) ; evaluates to 6727
puzzles for student presentations • puzzle #1 • define exp and compound using the procedure ‘repeated’ from problem set 2,and explain how they work • puzzle #2 • define procedures make-pair, first and second such that(first (make-pair a b)) evaluates to whatever a evaluates to, and(second (make-pair a b)) evaluates to whatever b evaluates to • hint: make-pair’s result is a procedure