CSC151 2007S, Class 33: Naming Local Values Admin: * Guest! * Are there questions on Assignment 12? * How do you get the color of the figure to depend upon the position? * In roll! (or roll-kernel!), reset the color before calling draw-rolled! (Alternately, set the color in draw-rolled!) * Reflections from those who finished already. * Warning! The three hour estimate was correct. * Reflections from your brilliant TA * Consider playing with step and rotation * It's okay if you can't explain everything * Reading for tomorrow: Local Procedure Bindings. * EC: Talk today at 4:30 on visualizing proteins Overview: * Review of evaluation * Why name things. * Techniques currently known for naming things. * Naming things with let. * Naming things with let*. * Naming procedures. * Lab. How does Scheme evaluate an expression, like (max (+ 2 3) (sqrt your-grade)) Note: Depends on the definition of max (define max (lambda (x y) (if (> x y) x y))) * First evaluate each argument of max * IN some cases, we need to use the same technique for evaluating the arguments (that is, if there is a procedure application, evaluate each argument in that application) * In some cases (e.g., "2"), the value is already evaluated * In some cases (e.g., "your-grade"), you must look up the value associated with the name (max 5 9) * Next, follow the instructions in the procedure * Update the name table so each formal parameter maps to the corresponding argument * Evaluate the body Why might Sam say that "evaluation is recursive?" * When we evaluate the whole expression, we must also evaluate subexpressions Why review how Scheme works? * Part of the evaluation process involves looking up names We know four primary ways to get name/value pairs into the "table" * (define name exp) evaluates expression and then pairs it with name in "the table" * (define$ name exp) * Rely on someone else having done it previously (use previously defined things) * Apply a procedure! Why care about names? * For procedures, naming a procedure clarifies the purpose ((lambda (lst) (/ (- (sum lst) (max lst)) (- (length lst) 1))) (list 90 85 86 10 100))a * Average of grades, after dropping highest score (define average-grades-after-dropping-highest-score (lambda (lst) (/ (- (sum lst) (max lst)) (- (length lst) 1)))) (average-grades-after-dropping-highest-score (list 90 85 86 10 100)) * Naming parameters lets you generalize procedures * Generalizing procedures saves time, since we don't have to rewrite code * Naming saves recompution (if (> (agadhs grades) max-grade) (throw "In spite of my evil grading system, you are still doing exceptionally well")) (assign-letter-grade (- (agadhs grades) 10)))) We need new techniques for assigning names Enter 'let' * Violates parenthesization rule * Everything needs parens (not violates) * To apply a procedure to parameters, surround by parens * Scheme assumes that something that comes immediately after an open paren is a procedure). LET VIOLATES THIS ASSUMPTION (let ((NAME EXP) (NAME EXP) ... (NAME EXP)) EXP) Behaves very much like procedure evaluation: * Evaluate all the expressions * Update the table to associate names with those values * Evaluate the body * Forget about the updates (let ((exam-1-grade 95) (exam-2-grade (max 80 (+ 23 11 22 18))) (participation-grade (* 100 (/ classes-attended 40)))) (+ (* exam-1-grade .2) (* exam-2-grade .2) (* participation-grade .6)))