Presentation is loading. Please wait.

Presentation is loading. Please wait.

TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Similar presentations


Presentation on theme: "TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010."— Presentation transcript:

1 TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010

2 July 12 2010TeachScheme, ReachJava 20102 Booleans ; = : number number -> boolean ; >, =, <= : similar ; string=? : string string -> boolean ; image=? : image image -> boolean ; not : boolean -> boolean ; and : boolean … -> boolean ; or : boolean … -> boolean

3 July 12 2010TeachScheme, ReachJava 20103 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)

4 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 12 2010TeachScheme, ReachJava 20104

5 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 12 2010TeachScheme, ReachJava 20105

6 July 12 2010TeachScheme, ReachJava 20106 Stopping an Animation There's another kind of handler: (stop-when function) where function has contract model -> boolean

7 July 12 2010TeachScheme, ReachJava 20107 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?))

8 July 12 2010TeachScheme, ReachJava 20108 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.

9 July 12 2010TeachScheme, ReachJava 20109 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.

10 July 12 2010TeachScheme, ReachJava 201010 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!"

11 July 12 2010TeachScheme, ReachJava 201011 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!")

12 July 12 2010TeachScheme, ReachJava 201012 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 ] ) )

13 July 12 2010TeachScheme, ReachJava 201013 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!" ] ))

14 July 12 2010TeachScheme, ReachJava 201014 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!" ] ))

15 July 12 2010TeachScheme, ReachJava 201015 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?")

16 July 12 2010TeachScheme, ReachJava 201016 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?" ] ))

17 July 12 2010TeachScheme, ReachJava 201017 Isomorphism The shape of the data determines the shape of the code and tests. Say this ten times before bed.

18 July 12 2010TeachScheme, ReachJava 201018 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"

19 July 12 2010TeachScheme, ReachJava 201019 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")

20 July 12 2010TeachScheme, ReachJava 201020 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 ] ))

21 July 12 2010TeachScheme, ReachJava 201021 Another example: body Fill in the answers (define (pepper-scale scoville) ; scoville a number (cond [ q "serrano" ] [ q "cayenne" ] [ q "thai" ] [ q "habanero" ] ))

22 July 12 2010TeachScheme, ReachJava 201022 Another example: body Fill in the questions (define (pepper-scale scoville) ; scoville a number (cond [ (and (>= scoville 5000) ( = scoville 30000) ( = scoville 65000) ( = scoville 100000) "habanero" ] ))

23 July 12 2010TeachScheme, ReachJava 201023 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")

24 July 12 2010TeachScheme, ReachJava 201024 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")

25 July 12 2010TeachScheme, ReachJava 201025 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".

26 July 12 2010TeachScheme, ReachJava 201026 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

27 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 13 201027TeachScheme, ReachJava 2010

28 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) July 13 201028TeachScheme, ReachJava 2010

29 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 13 201029TeachScheme, ReachJava 2010

30 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 13 201030TeachScheme, ReachJava 2010

31 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 July 13 201031TeachScheme, ReachJava 2010

32 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 13 201032TeachScheme, ReachJava 2010

33 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)) July 13 201033TeachScheme, ReachJava 2010

34 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 13 201034TeachScheme, ReachJava 2010

35 Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")…] [(key=? key "right")…] [else…] )) July 13 201035TeachScheme, ReachJava 2010

36 Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")(- x 1)] [(key=? key "right")(+ x 1)] [elsex] )) July 13 201036TeachScheme, ReachJava 2010

37 Running the animation (big-bang (/ WIDTH 2) (check-with number?) (on-draw calendar-at-x) (on-key handle-key)) July 13 201037TeachScheme, ReachJava 2010

38 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 13 201038TeachScheme, ReachJava 2010

39 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 13 201039TeachScheme, ReachJava 2010

40 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 13 201040TeachScheme, ReachJava 2010

41 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". July 13 201041TeachScheme, ReachJava 2010

42 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 13 201042TeachScheme, ReachJava 2010

43 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) July 13 201043TeachScheme, ReachJava 2010

44 Skeleton & inventory (define (big? thing) ; thing a string, number, or image ) July 13 201044TeachScheme, ReachJava 2010

45 Body, first draft (define (big? thing) ; thing string, number, or image (cond [ q a ] [ q a ] [ q a ] )) July 13 201045TeachScheme, ReachJava 2010

46 Body, second draft (define (big? thing) ; thing string, number, or image (cond [(string? thing) a ] [(number? thing) a ] [(image? thing) a ] )) July 13 201046TeachScheme, ReachJava 2010

47 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 13 201047TeachScheme, ReachJava 2010


Download ppt "TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010."

Similar presentations


Ads by Google