CSC151 2010S, Class 16: Anonymous Procedures Overview: * Review of Wednesday's class. * Summary of new material. * Questions. * Lab. Admin: * Chocolate * No reading for Monday! If all goes well, we'll go over the exam. If not, we'll do something else. * No class mentor today. * MathCounts results: 6/24 * Crud is going around campus. Please take care of yourselves: Get enough sleep. Eat well. Wash. Let me know if you can't make it to class. Phone calls resume next week. * EC (academic) for today's CS table. * EC (academic) for next Wednesday's talk on Open Information Culture (4:30 in Burling). * EC (academic) for next week's convocation (Catherine Keller). * Are there questions on Assignment 4? Assignment 4 * No questions Lists, Map, and Such * Make lists of things * A list is a collection of values * Organize information together * Lists often shorten the work we do * With the map procedure and lists, we can apply a procedure to lots and lots of values * Let's us generalize * Let's us write less code (more quickly) Questions (what puzzled you from Wednesday) * Is there a way to guarantee that everything in a list is the same kind of thing? * Yes. You'll soon learn how to write something that checks that. * Ordering is confusing * Practice * It remains difficult to "unpack" (find all of the parts and understand their relationships) - Try to work outside-in then inside-out. (visualize-colors (map (lambda (weight) (rgb-blend weight RGB-RED RGB-BLUE)) (map (r-s / 10) (reverse (iota 11)))) 11) * Expectation one: The first parameter to visualize-colors will be a list (of length 11) * That list will be created by map, which often has the form (map FUNCTION LIST) * (lambda (x) ...) DETOUR Defining functions (define red-blue-blend (lambda (weight) (rgb-blend weight RGB-RED RGB-BLUE))) (define x 3) (+ x x) => (+ 3 3) (map red-blue-blend (list 0 0.1 0.2 0.7 0.8)) => (map (lambda (weight) (rgb-blend weight RGB-RED RGB-BLUE))) (list 0 0.1 0.2 0.7 0.8)) * How do we read this? * First, when we see (lambda (PARAM) ...) we read this to ourselves as "A function whose parameter is * Second, when we see map, we read this as "substitute each value in the list, one-by-one, for the parameter, and evaluate" * So, we're going to substitute each value in the following for the weight (map (r-s / 10) (reverse (iota 11)))) * (r-s / 10) is shorthand for (lambda (x) (/ x 10)) * We're going to divide each element in a list by 10. * That list is the reverse of (iota 11) * (iota 11) is (0 1 2 3 4 5 ... 10) * reversed is (10 9 8 .. 2 1 0) * divided by ten is (1 .9 .8 .7 ... .1 0) * We use those as the weights in the blend Delayed questions * What's the best way to decide on the parameterse when setting up a function? * Think about what values you'll need in order to write the function. * Think about which of those values you can compute on your own (or that are fixed). * Example: Draw a line from top-left to bottom-right of an image * Need: top, left, bottom, right, and image * Know: top is 0, left is 0. * We might write (lambda (image right bottom) (image-draw-line image 0 0 right bottom)) * Example, continued: We can also compute the bottom and right if given the image * The right is the width minus 1 * The bottom is the height minus 1 * Can you make a list of procedures? Yes. You can make a list of any value you can represent in Scheme. * Can you make an infinite list? Not easily. * How smart is the Scheme interpreter? I'm not sure. It will normally build lists, even if you don't need all of the elements. * Can you explain the difference between l-s and r-s? * (r-s fun val) => (lambda (x) (fun x val)) * (l-s fun val) => (lambda (x) (fun val x)) (r-s - 1) => (lambda (x) (- x 1)) = "subtract 1 from x" (l-s - 1) => (lambda (x) (- 1 x)) = "subtract x from 1" > (map (r-s - 1) (iota 10)) (-1 0 1 2 3 4 5 6 7 8) > (map (l-s - 1) (iota 10)) (1 0 -1 -2 -3 -4 -5 -6 -7 -8) Other Ways to Make Procedures Lab