CSC151.02 2003F, Class 35: Higher-Order Procedures Admin: * Exam questions? * Read Higher-Order Procedures * Read More Higher-Order Procedures * Origami workshop on Saturday, 1:30-3:30 in South Lounge * Goal: Lots of discussion today, No pre-lab discussion tomorrow Overview: * Exercises * Patterns * Patterns as Procedures * Common Patterns * Anonymous Procedures * More Patterns * Returning Procedures Exam Issues: Q: Do you need to document e.c. problems? A: No, you don't need to document the problems. You also don't need to document the solutions. Q: How should we document the helper procedures A: Normal People: One or two sentences. Suck-ups: Six P's. Q: Six P's on number 1? A: No Q: Can you list all the things that you hate about our code? A: Penalties: Stupid variable names. Overly-short variable names. Code whose format does not reflect its structure. Lines that wrap (like this one). Explicit use of #t and #f. Stupid code, like (+ 0 x) instead of x. Nested ifs when conds will suffice. Spelling errors. Grammatical errors. (Others to be determined at the whim of the instructor.) A: No penalties (so far): People who claim that arriving at 2:15:01 is not technically late. People who fail to laugh at my jokes. TAs that forget what I hate. Q: How do you write each of the six P's? A: Three semicolons, space, the P-word, newline, three semicolons, three spaces, the corresponding text Assignments: Group West * Write a procedure that, given a list of numbers, creates a new list that contains only the odd numbers in the first list. Group Central: * Write a procedure that, given a list of Scheme values, creates a new list that contains only the numbers in the first list. Group East: * Write a procedure that, given a list of Scheme numbers, creates a new list that contains only the numbers greater than 100 in the first list. Observation: * The code looks almost identical * None of you check for errors * To avoid "stupid busywork", look at patterns (define FUNCTION (lambda (lst) (cond ((null? lst) null) ((TEST? (car lst)) (cons (car lst) (FUNCTION (cdr lst)))) (else (FUNCTION (cdr lst)))))) Cool idea: Why not make the TEST a parameter of the function Something you should know about let: it's just fancy substitution (let ((var exp)) (... var ...)) You can usually substitute exp for var (... exp ...) (let ((pi 3.14159) (radius 5)) (pi * radius * radius)) => (3.14159 * 5 * 5) In Scheme, you can write ANONYMOUS procedures, procedures without names. Things you've learned so far today: * Non-recursive procedures can be anonymous. (lambda (parms) body) * You can encapsulate common programming patterns in procedures that take other procedures as parameters * Example: select * These "pattern procedures" are typically called "higher-order" procedures. * Anonymous procedures are nice as parameters to the pattern procedures. What are some other common programming patterns you've seen? * Recursion over trees (somewhat complex) * Numeric recursion * Kinds of list recursion, other than extracting elements + Verifying some aspect of all elements of a list, e.g., all-real? + Tallying + Doing something to each element of a list + ... Good Scheme programmers regularly identify common patterns and rewrite them as higher-order procedures.