CSC302 2006S, Class 41: Back to Scheme Admin: * Attendance required on Friday * I still anticipate returning everything on Friday (as bad an idea as that is) * Email me your list of things for which you believe you deserve extra credit * About the final Overview: * Two problems. * Using higher-order functions. * Refactoring. Why Those Problems? * Goal of "understanding" functional programming How I will use your answers on those problems: * Really crappy answers: +5 on score on final exam * Really great answers: +10 on score on final exam * Everything else: Somewhere in between How a 151 or 152 student normally answers the first question (define update (lambda (lst) (if (null? lst) (cons (increment (square (car lst))) (update (cdr lst)))))) --- (define helper (lambda (x) (increment (square x)))) (define update (lambda (lst) (map helper lst))) --- (define update (let ((helper (lambda (x) (increment (square x))))) (lambda (lst) (map helper lst))) --- (define update (lambda (lst) (map (lambda (x) (increment (square x))) lst))) --- ; Assume ; (define compose (lambda (f g) (lambda (x) (f (g x))))) (define update (lambda (lst) (map (compose increment square) lst))) --- (define update (left-section map (compose increment square))) New Scheme (define update (cut map (compose increment square) <>)) -------------- Second problem: * Three nearly-identical procedures * Write the general one * Make them special cases of the general one One strategy for the second problem: ; Compute the list of integers from 1 to n (define intsTo (lambda (n) (if (zero? n) null (append (intsto (- n 1)) (list n))))) ; Can certainly be improved (define squaresTo (lambda (n) (map (lambda (x) (expt x 2)) (intsTo n)))) or (map (right-section expt 2) (intsTo n)) (define cubesTo (lambda (n) (map (lambda (x) (expt x 3)) (intsTo n)))) or (map (right-section expt 3) (intsTo n)) (define twosTo (lambda (n) (map (lambda (x) (expt 2 x)) (intsTo n)))) or (map (left-section expt 2) (intsTo n)) (define left-section (lambda (f left) (lambda (right) (f left right))))