CSC151 2007S, Class 45: Higher Order Procedures Admin: * Advance warning: Exam 3 will be distributed on Friday (or before). * Are there questions on the project? * Reading for Tuesday: Searching. * A few of you have asked about the final. * Our class includes both MWF 10:00-10:50 and TuTh 10:00-10:50 * We can have a final at 9:00 a.m. on Friday, 21 December 2007 * We can have a final at 2:00 p.m. on Wednesday, 19 December 2007 * I expect that most of you would prefer the Wednesday time. * I will be available for both (with different versions of the exam). * You may also take the exam during the other section's time, 2:00 p.m. on Tuesday, 18 December 2007. * If none of those times work, talk to me. * I'll ask you during Week 14 when (and if) you want to take the final. * EC: Percussion Ensemble 7:30 pm.ish Friday in Sebring-Lewis Overview: * Procedures as parameters. * Procedures as return values. * Writing map. * Writing all?. Why use Scheme? * We love parens :-) * Simple syntax, believe it or not * It's a leveling force: Almost no one knows Scheme before they take this class, even if they've had four years of high-school CS. * Scheme is a functional language * Functions can be data * That is, you can write and use procedures that take functions as parameters, that return functions as results, or even both * When have we had procedures as parameters? * map (the first parameter is a procedure) * foreach! (the first parameter is a procedure) * image.map, image.map! * region.compute-pixels! * left-section and right-section * How would we write our own procedures that take procedures as parameters? * Just have a parameter that you use like a procedure * How do you identify procedures in a chunk of code? * You see definitions using lambda * It appears in "the procedure position", immediately after an open paren (with a few exceptions) (define silly (lambda (foo) (foo 1 2 3))) ; Apply fun to each element in lst (define list.map (lambda (lst fun) ; Base case (if (null? lst) null (cons ; Do something to the car (fun (car lst)) ; Do something to the cdr (list.map (cdr lst) fun))))) list.map > (list.map (list 1 2 3) um) Error: eval: unbound variable: um > (list.map (list 1 2 3) (* 7 _)) Error: eval: unbound variable: > (let ((byseven (lambda (x) (* 7 x)))) (list.map (list 1 2 3) byseven)) (7 14 21) > (list.map (list 1 2 3) (lambda (x) (* 7 x))) (7 14 21) > (list.map (list 1 2 3) (left-section * 7)) (7 14 21) (define o (lambda (f g) ; We're returning a fucntion (lambda (x) (f (g x))))) > (o square square) > (define fun (o square square)) fun > fun > (fun 5) 625 > (define successor (lambda (x) (+ x 1))) successor > ((o square successor) 5) 36 > (list.map (list 1 2 3 4 5) (o square successor)) (4 9 16 25 36) > (list.map (list 1 2 3 4 5) (o successor square)) (2 5 10 17 26) > (list.map (list color.red color.green color.blue) (o rgb->cname rgb.darker)) ("red" "green" "blue") > (list.map (list color.red color.green color.blue) (o rgb->cname (o rgb.darker rgb.darker))) ("blood orange" "green" "blue") > (map (o car reverse) (list (list 1 2 3) (list 4 5 6) (list 7 8 9 10) (list 11 12))) (3 6 10 12)