470 likes | 489 Views
Understand Boolean functions and conditional statements in Scheme and Java. Learn to create functions and handle common beginner mistakes. Practice writing conditionals and error-checking for robust code.
E N D
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010
Booleans ; = : number number -> boolean ; >, <, >=, <= : similar ; string=? : string string -> boolean ; image=? : image image -> boolean ; not : boolean -> boolean ; and : boolean … -> boolean ; or : boolean … -> boolean TeachScheme, ReachJava 2010
A Boolean-valued function ; 18-to-25? : number -> boolean (define (18-to-25? age); age a number(and (>= age 18) (<= age 25))) (check-expect (18-to-25? 17) false) (check-expect (18-to-25? 18) true) (check-expect (18-to-25? 22) true) (check-expect (18-to-25? 25) true) (check-expect (18-to-25? 26) false) TeachScheme, ReachJava 2010
Type-checking animations Common beginner mistake in writing animations: handler returns wrong type. There's another kind of handler: (check-with function) ; function : anything -> boolean Built-in examples: image? number? string? etc. TeachScheme, ReachJava 2010
Type-checking animations Example: (big-bang 0 (check-with number?) (on-tick ...) (on-mouse ...) (on-draw ...)) Now if student writes a handler that returns the wrong type, there'll be a more-informative error message. The check-with clause also serves as documentation. TeachScheme, ReachJava 2010
Stopping an Animation There's another kind of handler: (stop-when function) where function has contract model -> boolean TeachScheme, ReachJava 2010
Stopping an Animation Example: a growing disk that stops growing when the radius reaches 100 ; model is a number representing radius ; over-100? : number -> boolean (define (over-100? r) (> r 100)) (big-bang 0 (check-with number?)(on-tick add1 1/4)(on-draw blue-circle-of-size)(stop-when over-100?)) TeachScheme, ReachJava 2010
Conditionals (cond[boolean-expr-1 answer-1][boolean-expr-2 answer-2]…[boolean-expr-n answer-n]) tries each boolean-expr in turn. As soon as one of them evaluates to true, it evaluates and returns the corresponding answer. TeachScheme, ReachJava 2010
Functions with conditionals Write a function reply that takes in one of the strings "good morning", "good afternoon", or "good night", and returns "I need coffee!", "I need a nap!", or "Bed time!" respectively. TeachScheme, ReachJava 2010
Functions with conditionalsContract & data analysis ; reply : string -> string Data analysis: the input falls into three categories: "good morning", "good afternoon", and "good night". The output likewise falls into three categories: "I need coffee!", "I need a nap!", or "Bed time!" TeachScheme, ReachJava 2010
Functions with conditionalsTest cases Need a test case for each category of input, and each category of output. Conveniently, they match up one-to-one in this example. (check-expect (reply "good morning")"I need coffee!") (check-expect (reply "good afternoon")"I need a nap!") (check-expect (reply "good night")"Bed time!") TeachScheme, ReachJava 2010
Functions with conditionalsSkeleton & inventory Since there are three categories of input (and output), we'll probably need a 3-branch cond: (define (reply greeting); greeting ; a string(cond [ question answer ] [ question answer ] [ question answer ]) ) TeachScheme, ReachJava 2010
Functions with conditionalsBody Fill in either all three answers, or all three questions, whichever is easier. In this case, the answers. (define (reply greeting); greeting ; a string(cond [ question "I need coffee!" ] [ question "I need a nap!" ] [ question "Bed time!" ])) TeachScheme, ReachJava 2010
Functions with conditionalsBody Then do the other of (questions, answers). (define (reply greeting); greeting ; a string(cond [ (string=? greeting "good morning") "I need coffee!" ] [ (string=? greeting "good afternoon") "I need a nap!" ] [ (string=? greeting "good night") "Bed time!" ])) TeachScheme, ReachJava 2010
Functions with conditionalsError-checking Quibble: this isn't idiot-proof. What happens if input isn't one of the three recognized inputs? Answer: ugly error message. Solution: revise data analysis (and everything that depended on it) Input is "good morning", "good afternoon", "good night", or anything else. Output is "I need coffee!", "I need a nap!", "Bed time!", or "Huh?" Add one more test case (check-expect (reply "buenas noches") "Huh?") TeachScheme, ReachJava 2010
Functions with conditionalsError-checking Add one more cond case: (define (reply greeting); greeting ; a string(cond [ (string=? greeting "good morning") "I need coffee!" ] [ (string=? greeting "good afternoon") "I need a nap!" ] [ (string=? greeting "good night") "Bed time!" ][ else "Huh?" ])) TeachScheme, ReachJava 2010
Isomorphism The shape of the data determines the shape of the code and tests. Say this ten times before bed. TeachScheme, ReachJava 2010
Another example ; pepper-scale : number -> string ("serrano", "cayenne", "thai", "habanero") ; Scoville 5000-25000 -> serrano ; Scoville 30000-50000 -> cayenne ; Scoville 65000-90000 -> thai ; Scoville 100000-up -> habanero ; Data analysis: input is a number, but falls into 4 categories: ; 5000-25000, 30000-50000, 65000-90000, 100000-up. ; Output is likewise four categories: "serrano", "cayenne", "thai"., "habanero" TeachScheme, ReachJava 2010
Another example ; Test cases therefore need to include all 4 categories plus borderlines. (check-expect (pepper-scale 5000) "serrano") (check-expect (pepper-scale 16500) "serrano") (check-expect (pepper-scale 25000) "serrano") (check-expect (pepper-scale 30000) "cayenne") (check-expect (pepper-scale 42000) "cayenne") (check-expect (pepper-scale 50000) "cayenne") (check-expect (pepper-scale 65000) "thai") (check-expect (pepper-scale 85000) "thai") (check-expect (pepper-scale 90000) "thai") (check-expect (pepper-scale 100000) "habanero") (check-expect (pepper-scale 120000) "habanero") TeachScheme, ReachJava 2010
Another example: skeleton & inventory There are four categories, hence a four-branch cond: (define (pepper-scale scoville); scoville a number(cond [ q a ] [ q a ] [ q a ] [ q a ])) TeachScheme, ReachJava 2010
Another example: body Fill in the answers (define (pepper-scale scoville); scoville a number(cond [ q "serrano" ] [ q "cayenne" ] [ q "thai" ] [ q "habanero" ])) TeachScheme, ReachJava 2010
Another example: body Fill in the questions (define (pepper-scale scoville); scoville a number(cond [ (and (>= scoville 5000) (<= scoville 25000)) "serrano" ] [ (and (>= scoville 30000) (<= scoville 50000)) "cayenne" ] [ (and (>= scoville 65000) (<= scoville 90000)) "thai" ] [ (>= scoville 100000) "habanero" ])) TeachScheme, ReachJava 2010
Another example ; rough-age : number -> string ("child", "teenager", or "adult") ; Data analysis: input is a number, but falls into 3 categories: ; under 13, 13-19, and over 19. ; Output is likewise three categories: "child", "teenager", "adult". ; Test cases therefore need to include all 3 categories plus borderlines. (check-expect (rough-age 7) "child") (check-expect (rough-age 13) "teenager") (check-expect (rough-age 16.3) "teenager") (check-expect (rough-age 19) "teenager") (check-expect (rough-age 20) "adult") TeachScheme, ReachJava 2010
Functions with conditionals ; rough-age : number -> string ("child", "teenager", or "adult") ; Data analysis: input is a number, but falls into 3 categories: ; under 13, 13-19, and over 19. (define (rough-age age); age a number(cond [(< age 13) "child"] [(and (>= age 13) (<= age 19)) "teenager"] [(> age 19) "adult"])) (check-expect (rough-age 7) "child") (check-expect (rough-age 13) "teenager") (check-expect (rough-age 16.3) "teenager") (check-expect (rough-age 19) "teenager") (check-expect (rough-age 20) "adult") TeachScheme, ReachJava 2010
We could have written… (define (rough-age age); age a number(cond [(< age 13) "child"] [(<= age 19) "teenager"] [else "adult"])) by relying on the fall-through behavior of the conditional. Advantage: less typing. Advantage: save a few nanoseconds of run time (maybe). Disadvantage: you can't tell when a particular branch will happen just by looking at that condition; you have to also look at all the previous ones Disadvantage: branches of conditional can no longer be reordered without changing function behavior. Disadvantage: the isomorphism to the input data type is less clear. Use else only when you really mean "anything else". TeachScheme, ReachJava 2010
Animations with conditionals Can now assign animations that decide among a finite set of cases, e.g. • slide show of a sequence of pictures • stop light that cycles red, green, yellow, red… • See PP chap. 17 for lots of examples TeachScheme, ReachJava 2010
Mouse handlers • Contract : model num(x) num(y) string-> model • Events: "button-down", "button-up", "drag", "move", "enter", "leave" … • Typically, one or two "interesting" events and an "anything else" category. • Code structure: cond with one or two (string=? ... …) questions and an else TeachScheme, ReachJava 2010
Example mouse handler Exercise 18.1.1 in book: ; add-dot-on-mouse-down : image(old) num(x) num(y) string(event) -> image (check-expect (add-dot-on-mouse-down BACKGROUND 35 10 "button-down")(place-image DOT 35 10 BACKGROUND)) (check-expect (add-dot-on-mouse-down BACKGROUND 35 10 "button-up")BACKGROUND) TeachScheme, ReachJava 2010
Example mouse handler (define (add-dot-on-mouse-down old x y event); old image; x, y numbers; event string(cond [(string=? event "button-down") (place-image DOT x y old)] [else old] ; ignore this event)) TeachScheme, ReachJava 2010
A new type: key A key is a string: either a 1-character string like "r", "7", "F", or a longer string specifying a special key: "left", "right", "down", "up", "escape", "f1", "home", …) Built-in function ; key=? : key key -> boolean TeachScheme, ReachJava 2010
Example Write an animation of a calendar that starts at the middle of the screen and moves left or right in response to left and right arrow keys on the keyboard Ex. 18.2.1 in book TeachScheme, ReachJava 2010
Animation: model & handlers Let's use a number as the model, representing the x coordinate (the y coordinate will be fixed at, say, 50) We need a redraw handler ; calendar-at-x: number -> image and a key handler ; handle-key: number key -> number TeachScheme, ReachJava 2010
Animation: draw handler ; calendar-at-x: number -> image (define (calendar-at-x x); x number(place-image calendar x 50 BACKGROUND)) (check-expect (calendar-at-x 27) (place-image calendar 27 50 BACKGROUND)) (check-expect (calendar-at-x 193) (place-image calendar 193 50 BACKGROUND)) TeachScheme, ReachJava 2010
Animation: key handler Model is a number representing x coordinate Want to respond to left, right arrow keys ; handle-key : number(x) key -> number (check-expect (handle-key 10 "D") 10) (check-expect (handle-key 10 "left") 9) (check-expect (handle-key 10 "right") 11) (check-expect (handle-key 10 "up") 10) TeachScheme, ReachJava 2010
Animation: key handler (define (handle-key x key); x number; key key(cond [(key=? key "left") …] [(key=? key "right") …] [else …])) TeachScheme, ReachJava 2010
Animation: key handler (define (handle-key x key); x number; key key(cond [(key=? key "left") (- x 1)] [(key=? key "right") (+ x 1)] [else x])) TeachScheme, ReachJava 2010
Running the animation (big-bang (/ WIDTH 2) (check-with number?) (on-draw calendar-at-x) (on-key handle-key)) TeachScheme, ReachJava 2010
Defining a type "by choices" • Type = "this string or that string or …" • Type = "this range of numbers or that range of numbers or …" • Type = "this type or that type or …" • Test cases: at least one for each category of input or output TeachScheme, ReachJava 2010
Code for definition by choices • A cond w/ N branches, where N is the number of categories in the input type • For strings, usually use string=? • For numbers, <, >, <=, >=, =, … • For keys, key=? • For types, …? TeachScheme, ReachJava 2010
Discriminator functions (built-in) • number? : anything -> boolean • string? : anything -> boolean • image? : anything -> boolean • key? : anything -> boolean • etc. Tell whether something is of the specified type TeachScheme, ReachJava 2010
Example Write a function big? that takes in a number or a string or an image. Numbers > 1000 are "big"; strings longer than 10 characters are "big"; images with height* width > 10000 are "big". TeachScheme, ReachJava 2010
Contract & data analysis ; big? : number, string, or image -> boolean ; Data analysis: input is either number, string, or image ; In each case, it's broken down into two sub-categories TeachScheme, ReachJava 2010
Test cases (check-expect (big? 347) false) (check-expect (big? 1000) false) (check-expect (big? 1200) true) (check-expect (big? "alison") false) (check-expect (big? "passphrase") false) (check-expect (big? "brontosaurus") true) (check-expect (big? calendar) false) (check-expect (big? (rectangle 200 50 "solid" "purple")) false) (check-expect (big? (ellipse 200 60 "outline" "chartreuse")) true) TeachScheme, ReachJava 2010
Skeleton & inventory (define (big? thing); thing a string, number, or image) TeachScheme, ReachJava 2010
Body, first draft (define (big? thing); thing string, number, or image(cond [ q a ] [ q a ] [ q a ])) TeachScheme, ReachJava 2010
Body, second draft (define (big? thing); thing string, number, or image(cond [(string? thing) a ] [(number? thing) a ] [(image? thing) a ])) TeachScheme, ReachJava 2010
Body, third draft (define (big? thing); thing string, number, or image(cond [(string? thing) (> (string-length thing) 10)] [(number? thing)(> thing 1000)] [(image? thing)(> (* (image-width thing) (image-height thing)) 10000)])) TeachScheme, ReachJava 2010