TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010
July TeachScheme, ReachJava Booleans ; = : number number -> boolean ; >, =, <= : similar ; string=? : string string -> boolean ; image=? : image image -> boolean ; not : boolean -> boolean ; and : boolean … -> boolean ; or : boolean … -> boolean
July TeachScheme, ReachJava 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)
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. July TeachScheme, ReachJava 20104
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. July TeachScheme, ReachJava 20105
July TeachScheme, ReachJava Stopping an Animation There's another kind of handler: (stop-when function) where function has contract model -> boolean
July TeachScheme, ReachJava 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?))
July TeachScheme, ReachJava 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.
July TeachScheme, ReachJava 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.
July TeachScheme, ReachJava Functions with conditionals Contract & 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!"
July TeachScheme, ReachJava Functions with conditionals Test 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!")
July TeachScheme, ReachJava Functions with conditionals Skeleton & 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 ] ) )
July TeachScheme, ReachJava Functions with conditionals Body 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!" ] ))
July TeachScheme, ReachJava Functions with conditionals Body 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!" ] ))
July TeachScheme, ReachJava Functions with conditionals Error-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?")
July TeachScheme, ReachJava Functions with conditionals Error-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?" ] ))
July TeachScheme, ReachJava Isomorphism The shape of the data determines the shape of the code and tests. Say this ten times before bed.
July TeachScheme, ReachJava Another example ; pepper-scale : number -> string ("serrano", "cayenne", "thai", "habanero") ; Scoville > serrano ; Scoville > cayenne ; Scoville > thai ; Scoville up -> habanero ; Data analysis: input is a number, but falls into 4 categories: ; , , , up. ; Output is likewise four categories: "serrano", "cayenne", "thai"., "habanero"
July TeachScheme, ReachJava 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 ) "habanero") (check-expect (pepper-scale ) "habanero")
July TeachScheme, ReachJava 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 ] ))
July TeachScheme, ReachJava Another example: body Fill in the answers (define (pepper-scale scoville) ; scoville a number (cond [ q "serrano" ] [ q "cayenne" ] [ q "thai" ] [ q "habanero" ] ))
July TeachScheme, ReachJava Another example: body Fill in the questions (define (pepper-scale scoville) ; scoville a number (cond [ (and (>= scoville 5000) ( = scoville 30000) ( = scoville 65000) ( = scoville ) "habanero" ] ))
July TeachScheme, ReachJava 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")
July TeachScheme, ReachJava 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) ( 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")
July TeachScheme, ReachJava 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".
July TeachScheme, ReachJava 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
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 July TeachScheme, ReachJava 2010
Example mouse handler Exercise in book: ; add-dot-on-mouse-down : image(old) num(x) num(y) string(event) -> image (check-expect (add-dot-on-mouse-down BACKGROUND "button-down") (place-image DOT BACKGROUND)) (check-expect (add-dot-on-mouse-down BACKGROUND "button-up") BACKGROUND) July TeachScheme, ReachJava 2010
Example mouse handler (define (add-dot-on-mouse-down old x y event) ; oldimage ; x, ynumbers ; eventstring (cond [(string=? event "button-down") (place-image DOT x y old)] [else old]; ignore this event )) July 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 July 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 in book July 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 July 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 BACKGROUND)) (check-expect (calendar-at-x 193) (place-image calendar BACKGROUND)) July 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) July TeachScheme, ReachJava 2010
Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")…] [(key=? key "right")…] [else…] )) July TeachScheme, ReachJava 2010
Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")(- x 1)] [(key=? key "right")(+ x 1)] [elsex] )) July TeachScheme, ReachJava 2010
Running the animation (big-bang (/ WIDTH 2) (check-with number?) (on-draw calendar-at-x) (on-key handle-key)) July 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 July 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, …? July 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 July 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 > are "big". July 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 July 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 "solid" "purple")) false) (check-expect (big? (ellipse "outline" "chartreuse")) true) July TeachScheme, ReachJava 2010
Skeleton & inventory (define (big? thing) ; thing a string, number, or image ) July TeachScheme, ReachJava 2010
Body, first draft (define (big? thing) ; thing string, number, or image (cond [ q a ] [ q a ] [ q a ] )) July TeachScheme, ReachJava 2010
Body, second draft (define (big? thing) ; thing string, number, or image (cond [(string? thing) a ] [(number? thing) a ] [(image? thing) a ] )) July 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)] )) July TeachScheme, ReachJava 2010