CSC151.01 2006S, Class 20: Introduction to Local Bindings Admin: * If current events are affecting your ability to work, talk to me and we'll work something out. * EC: Campus Musician Showcase, 9 p.m. Thursday, Bob's. (Vince at 10:40 p.m.) * Today's outline has much more information than I plan to cover in class. You may find it useful to review that reading. * Reading: Local Procedure Bindings Overview: * Exam questions * Summary of reading. * Lab. /Exam Questions/ * None /Learned from Reading/ * There's a fancy-schmancy keyword called let * A lot like define - Gives you a way to name values * It's local - The names can only be used within the body of let. * It permits multiple simultaneous definitions * It's temporary - Once the body is evaluated, the names are "unbound" * Structure of let (let binding-list body) (let ((name exp) (name exp) (name exp)) body) * Meaning of let (let ((name0 exp0) (name1 exp1) ... (namen expn)) body) 1. Evaluate exp0 ... expn 2. Binds name0 to the value of exp0, name1 to the value of exp1, ... 3. Evaluates the body 4. Forgets the bindings 5. Returns the value of the last expression in the body (let ((scale-factor 1.2)) (* grade scale-factor)) * Position of let (define proc (let binding-list (lambda (...) ...))) (define proc (lambda (...) (let binding-list body))) * Questions the reading did not answer * When should I use define, when should I use let? * If you only need the name within one procedure, use let. * If you need the name in multiple procedures, use define. * If your procedure will fail if the name is associated with the wrong value, use let (define pi 3.14) (define circ-of-circle (lambda (radius) (* 2 pi radius))) Vs. (define circ-of-circle (let ((pi ...)) ...) * If you worry that others will know about your local names. * What's the advantage of let over let*? * A bit faster * Different situations call for different strategies /Lab/ * The only thing Sam remembers from High School physics (other than a probably false memory of listening to National Lampoon's "Catch it and Your Keep It"): An Oldsmobile Delta 88 goes 60 miles per hour 88 * 60 = 5280 * Obligatory joke disclaimer: The following joke relies upon cultural stereotypes. As I learned the joke from a member of the stereotyped group, because I am at least partially a member of that group, and because the joke only uses a small bit of that stereotype, I feel comfortable making it. Please do not be offended.