CSC151 2007S, Class 18: Iterating Over Lists Admin: * Don't jump off the loggia into the snow. It's stupid. * The DrFu Web site now has links to instructions for running DrFu remotely on your PC or Linux box. Let me know if they work okay. * I think everyone got their homework in by midnight last night. Thanks! * Ian Athanasakis will present a talk on his work at Google on Thursday at 4:30 in 3821. Extra credit for attending. * Friday's reading on local bindings will not be available until tonight or tomorrow. * I'll reserve a few minutes at the start of class to consider conditionals. Overview: * Review: How Scheme evaluates expressions. * Repetition. * Building new lists from old with map. * Anonymous procedures. * Doing something with each value in a list with foreach!. * Drawing lists of spots. ==Evaluating Expressions== Sequence one (define x 5) (define y 3) Name Val +-----+---+ | x | 5 | +-----+---+ | y | 3 | +-----+---+ (+ x y) Look up + in the table Look up x in the table Look up y in the table (code-for-+ 5 3) => 8 Outputs 8 Sequence two (define x 5) (define y 3) +-----+---+ | x | 5 | +-----+---+ | y | 3 | +-----+---+ (define z x) +-----+---+ | x | 5 | +-----+---+ | y | 3 | +-----+---+ | z | 5 | +-----+---+ (define x 2) +-----+---+ | x | 2 | +-----+---+ | y | 3 | +-----+---+ | z | 5 | +-----+---+ (+ x y z) Look up everything (code-for-+ 2 3 5) => 10 Sequence three (define x 5) (define y 7) (define fun (lambda (x) (+ x y))) +-----+---+ | x | 5 | +-----+---+ | y | 7 | +-----+---+ | fun | (lambda (x) (+ x y)) | +-----+---+ (fun 3) Look up fun Surpisingly, fun is not "CSC151", but rather a lambda expression ((lambda (x) (+ x y)) 3) Plug in 3 for x in the body of the lamda (+ 3 y) Look up + and y in the table (code-for-plus 3 7) 10 A broader definition ((lambda (x) (+ x y)) 3) Update the table to add a second entry for x +-----+---+ | x | 5 | +-----+---+ | y | 7 | +-----+---+ | fun | (lambda (x) (+ x y)) | +-----+---+ +-----+---+ | x | 3 | +-----+---+ (+ x y) Look up + , x, and y x is in "new definitions pane" y is not (code-for-+ 3 7) => 10 Remove that extra definition +-----+---+ | x | 5 | +-----+---+ | y | 7 | +-----+---+ | fun | (lambda (x) (+ x y)) | +-----+---+ Sequence 4 (define y 7) (define fun (lambda (x) (+ x y))) (define morefun fun) +-----+---+ | fun | (lambda (x) (+ x y)) | +-----+---+ | morefun | (lambda (x) (+ x y)) | +-----+---+ (define fun (lambda (a) 5)) +-----+---+ | fun | (lambda (a) 5) | +-----+---+ | morefun | (lambda (x) (+ x y)) | +-----+---+ (fun 1) (morefun 1) ---- Common components of algorithms * Named values * Basic operations * Sequencing of operations * Put one after the other * Nest them * Procedures: Encapsulate and parameterize a group of operations "Encapsulate" -> Put into a group that you use and think of as a whole "Parameterize" -> Add and name inputs to the procedure * Choice * if * cond * (maybe with and and or?) * Repetition/Iteration * Do things multiple times * Fixed number of times * Until some condition holds Today: Iterating with Lists * "Repeatedly do something with each element of a list" + Model one: Pure - Does not change any of the parameters (or anything, for that matter): Called only to compute a new value * Eg. +, spot-nudge-right + Model two: Impure: Goal is to change something * image-set-pixel!, image-render-something!, turtle-forward! * Tradition in Scheme: impure procedures end with exclamation point * Pure way of apply function to list Input: fun, list (v1 v2 ... vn) Output: list ((fun v1) (fun v2) ... (fun vn)) * Impure way Input: fun!, list (v1 v2 ... vn) Output: Don't care Change acheived by (fun! v1) (fun! v2) (fun! v3) ... (fun! v4) * Two models -> two procedures (map fun lst) - Pure (foreach! fun! lst) - Impure