One-Slide Summary • In Scheme, expressions evaluate to values. Five evaluation rules describe this process. • Lambda means “make a function”. A lambda expression specifies the formal parameter and the function body. • Evaluating a function application involves evaluating the function, finding its body, replacing the formal parameters with the The Value of Everything evaluated actual arguments, and evaluating the result. & Procedure Practice #1 #2 Lecture Outline Lab and Office Hours • Staffed Lab Hours • Survey Responses – Monday 12:30-13:30 (Small Hall) – Monday 14:00-15:00 (Small Hall) • Evaluation Rules – Monday 17:00-19:30 (Thornton Stacks) – Lambda – Tuesday 11:00-12:30 (Olsson 001) – Wednesday 10:30-13:00 (Thornton Stacks) • Problem Set 1 – Thursday 10:00-12:30 (Thornton Stacks) – Decent Scheme – Sunday 13:00-17:00 (Olsson 001) • Office Hours – M W 13:30-14:00 (Olsson 219) #3 #4 How To Use Lab Hours The Forum! • Read the problems on your own and try them out first. • Your questions for – You can not just go to a TA and say “I don't get it, what do I do.” me are answered on •The TA is allowed to send you away. the forum. – You must demonstrate about five minutes worth of work: either on scratch paper, or with code you've • Any questions right tried and commented out. now? – For example: how would you do it in English? – As of 5pm yesterday, • Talk to your friends. there were already 4 • Do not expect to finish the Problem Sets in just the perfect scores and 1 staffed lab time. other submission on – They take longer. You must do much work alone. PS1. #5 #6
Who Are You? Problem Set 1 • I am a weird frisbee player who likes to learn • Scheme's Evaluation Rules tell you how to new things, plays volleyball, enjoys cycling, find the value of any expression. eats in class, shoots skeet, used to be afraid of • Questions 1 and 2 ask you to evaluate computers, loves “Lost”, likes trampolines, Scheme expressions in your mind snowboards, row on the UVA team, like sour – This is a popular exam question. candy, is certified for SCUBA, who is looking • Without Evaluation Rules: guesswork forward to attending my office hours. • Once you know the Evaluation Rules, you – CS 150 Gestalt Student, Spring 2010 can answer without any guessing! #7 #8 Evaluation Rules Primitive Examples • Primitives (* 55 66) – Evaluate to their pre-defined values What do these 5 • Names (+ x 2) evaluate to? – Evaluate to the value associated with that name -88 • Application (square-root 144) #t – Eval all sub-expressions. Apply the value of the first (a function) to the values of the others. #f • Lambda (lambda (x) (* x x x)) + – Evaluates to a function with parameters and body • If (if (< 3 5) 99 11) – Eval predicate. If #f, eval second option. Otherwise, eval the first option. #9 #10 Primitive Examples Name Examples 55 --> 5 (define x 55) -88 --> -88 (define y 66) #t --> true (#t) What do these #f --> false (#f) x evaluate to? + --> primitive addition y z #11 #12
Name Examples Application Examples What do these evaluate to? (define x 55) (sqrt 16) (define y 66) (abs -5) (string-length “Hi”) x --> 55 (+ 1 2) y --> 66 (+ 1 2 3) z --> reference to undefined identifier: z (+ 1) #13 #14 Application Examples Liberal Arts Trivia: Antropology (sqrt 16) --> 4 • This American cultural anthropologist is famous for her studies of Samoa and her (abs -5) --> 5 reports about the purportedly healthy attitude towards sex in South Pacific and Southeast (string-length “Hi”) --> 2 Asian traditional cultures, which influenced the women's liberation movement (e.g., by (+ 1 2) --> 3 claiming that females dominated in Chambri (+ 1 2 3) --> 6 and Papau New Guinea without problems). Five years after she died, her work was challenged (+ 1) --> 1 by Derek Freeman. #15 #16 Liberal Arts: Slavic Folklore Lambda • This witch-like character in Slavic folklore lives • Lambda means “make a function”. in a walking house with chicken feet (but no • Consider: cube(x) = x * x * x windows and no doors), flies around on a giant • Scheme-y: cube(x) = (* x x x) mortar, and kidnaps (presumably to eat) small children. Modest Mussorgsky's Pictures at an • Lambda: cube = (lambda (x) (* x x x)) Exhibition , a piano suite composed in 1874, features "The Hut on Bird's Legs" as its • Pure Scheme: penultimate movement. (define cube (lambda (x) (* x x x))) #17 #18
Anatomy Of A Function Lambda Examples • (define cube (lambda (x) (* x x x))) (define cube (lambda (x) (* x x x))) (define foo (lambda (p q) (+ p q))) formal parameters function body • (cube 5) (define bar (lambda (a b c) (* a c))) (cube 3) actual arguments function application • To evaluate a function application , replace it What do these (foo 5 6) evaluate to? with the function body, and then replace every formal parameter with its corresponding actual (bar 4 5 6) argument. (foo (cube 3) 1) • (cube 5) -> (* x x x) -> (* 5 5 5) -> 125 #19 #20 Lambda Examples Lambda Lambda Lambda (define cube (lambda (x) (* x x x))) • Consider these two functions: – (define cube (lambda (x) (* x x x))) (define foo (lambda (p q) (+ p q))) – (define cube (lambda (y) (* y y y))) (define bar (lambda (a b c) (* a c))) • Are they different? (cube 3) --> (* 3 3 3)->27 • Consider: (foo 5 6) --> (+ 5 6) -> 11 – (define nail (lambda (x y) (+ x y))) – (define polish (lambda (y x) (/ y x))) (bar 4 5 6) --> (* 4 6) -> 24 • What is: (foo (cube 3) 1) --> ... -> 28 Do this now – (polish (nail 6 4) 2) on paper! #21 #22 Sally Hansen does Lambda If Examples (define nail (lambda (x y) (+ x y))) (if #t “yes” “no”) (define polish (lambda (y x) (/ y x))) What do these evaluate to? (polish (nail 6 4) 2) (if #f “yes” “no”) • This is a call to polish with tricky arguments. (if (< 3 5) “ant” “bat”) • Recall the rule: evaluate the arguments first. (if (< 5 3) “cat” “dog”) – Argument 1: (nail 6 4) -> (+ x y) -> (+ 6 4) -> 10 – Argument 2: 2 -> 2 (if “x” “y” “z”) • Now take polish 's body, and replace the formal parameters with the actual arguments: (if (if 11 #f #t) 22 33) Why not – (/ y x) -> (/ 10 2) -> 5 (/ 2 10) ? #23 #24
If Examples Scheme Trickery • (100 + 100) (if #t “yes” “no”) -> “yes” – Error: The expression in the first position must be a function (or something special like if ). 100 is not a (if #f “yes” “no”) -> “no” function. (if (< 3 5) “ant” “bat”) -> “ant” • (if (not “batterie”) “fouetté” “plié”)) – “plié”. (not “batterie”) returns #f , because (if (< 5 3) “cat” “dog”) -> “dog” “batterie” is not #f . • (define (not v) (if v #f #t)) (if “x” “y” “z”) -> “y” • Does (if X #t #f) always equal X ? (if (if 11 #f #t) 22 33) -> 33 – Yes for #t, #f, (< 3 5), (> 5 6). – No for 3, 17, “hello”. #25 #26 Now You Know All of Scheme! Evaluating Eval expressions Eval • Once you understand Eval and Apply , you and Applying can understand all Scheme programs! functions are defined in terms • Except: of each other. – There are many primitives, and you need to know their predefined meaning. Without Eval, Apply Apply – There are a few more special forms (like if ). there would be – We have not define the evaluation rules no Apply, precisely enough to unambiguously Without Apply understand all programs (e.g., what does there would be “value associated with a name” mean?). no Eval! #27 #28 Now On To Problem Set 1 brighter? (define brighter? (lambda (color1 • Smooth transition ... color2) (if (> (+ (get-red color1) (get-green color1) (get-blue color1) ) (+ (get-red color2) (get-green color2) (get-blue color2)) #t #f))) Maybe...but very hard to tell. Your code should appear in a Is this correct? way that reveals its structure #29 #30
Brighter brighter? ? (define brighter? (lambda (color1 color2) (if (> (+ (get-red color1) (get-green color1) (define brighter? (get-blue color1)) (lambda (color1 color2) (+ (get-red color2) (> (+ (get-red color1) (get-green color1) (get-green color2) (get-blue color1)) (get-blue color2)) (+ (get-red color2) (get-green color2) #t #f))) (get-blue color2))))) What can we do about this duplicated code? Use [Tab] in DrScheme to line up your code structurally! #31 #32 Brighter brighter? ? Believable brighter? ? (define brightness (lambda (color) (+ (get-red color) (get-green color) (get-blue color)))) (define brighter? (lambda (color1 color2) (> (brightness color1) (brightness color2))) (make-color 255 255 0) (make-color 255 1 255) #33 #34 Color Absorbed Cognitive Scientist’s Answer (define brightness (lambda (color) (+ (* 0.299 (get-red color)) (* 0.587 (get-green color)) (* 0.114 (get-blue color))))) (define brighter? (lambda (color1 color2) (> (brightness color1) (brightness color2))) http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/OWENS/LECT14/ #35 #36
Recommend
More recommend