Top Banner
TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010
47
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

TeachScheme, ReachJava

Adelphi University

Tuesday morning

July 13, 2010

Page 2: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 2

Booleans

; = : number number -> boolean

; >, <, >=, <= : similar

; string=? : string string -> boolean

; image=? : image image -> boolean

; not : boolean -> boolean

; and : boolean … -> boolean

; or : boolean … -> boolean

Page 3: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 3

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)

Page 4: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Type-checking animationsCommon 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 2010 TeachScheme, ReachJava 2010 4

Page 5: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Type-checking animationsExample:(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 2010 TeachScheme, ReachJava 2010 5

Page 6: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 6

Stopping an Animation

There's another kind of handler:

(stop-when function)

where function has contract model -> boolean

Page 7: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 7

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?))

Page 8: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 8

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.

Page 9: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 9

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.

Page 10: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 10

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!"

Page 11: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 11

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!")

Page 12: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 12

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 ]) )

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

July 12 2010 TeachScheme, ReachJava 2010 13

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!" ]))

Page 14: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 14

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!" ]))

Page 15: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 15

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?")

Page 16: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 16

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?" ]))

Page 17: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 17

Isomorphism

The shape of the data determines the shape of the code and tests.

Say this ten times before bed.

Page 18: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 18

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"

Page 19: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 19

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")

Page 20: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 20

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 ]))

Page 21: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 21

Another example: body

Fill in the answers

(define (pepper-scale scoville); scoville a number(cond [ q "serrano" ] [ q "cayenne" ] [ q "thai" ] [ q "habanero" ]))

Page 22: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 22

Another example: bodyFill 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" ]))

Page 23: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 23

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")

Page 24: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 24

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")

Page 25: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 25

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".

Page 26: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

July 12 2010 TeachScheme, ReachJava 2010 26

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

Page 27: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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

July 13 2010 27TeachScheme, ReachJava 2010

Page 28: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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)

July 13 2010 28TeachScheme, ReachJava 2010

Page 29: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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))

July 13 2010 29TeachScheme, ReachJava 2010

Page 30: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 30TeachScheme, ReachJava 2010

Page 31: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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

July 13 2010 31TeachScheme, ReachJava 2010

Page 32: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Animation: model & handlersLet'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 -> imageand a key handler; handle-key: number key -> number

July 13 2010 32TeachScheme, ReachJava 2010

Page 33: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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))

July 13 2010 33TeachScheme, ReachJava 2010

Page 34: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 34TeachScheme, ReachJava 2010

Page 35: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Animation: key handler

(define (handle-key x key); x number; key key(cond [(key=? key "left") …] [(key=? key "right") …] [else …]))

July 13 2010 35TeachScheme, ReachJava 2010

Page 36: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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]))

July 13 2010 36TeachScheme, ReachJava 2010

Page 37: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Running the animation

(big-bang (/ WIDTH 2)

(check-with number?) (on-draw calendar-at-x)(on-key handle-key))

July 13 2010 37TeachScheme, ReachJava 2010

Page 38: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 38TeachScheme, ReachJava 2010

Page 39: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 39TeachScheme, ReachJava 2010

Page 40: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 40TeachScheme, ReachJava 2010

Page 41: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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".

July 13 2010 41TeachScheme, ReachJava 2010

Page 42: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 42TeachScheme, ReachJava 2010

Page 43: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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)

July 13 2010 43TeachScheme, ReachJava 2010

Page 44: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Skeleton & inventory

(define (big? thing); thing a string, number, or image)

July 13 2010 44TeachScheme, ReachJava 2010

Page 45: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Body, first draft

(define (big? thing); thing string, number, or image(cond [ q a ] [ q a ] [ q a ]))

July 13 2010 45TeachScheme, ReachJava 2010

Page 46: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Body, second draft

(define (big? thing); thing string, number, or image(cond [(string? thing) a ] [(number? thing) a ] [(image? thing) a ]))

July 13 2010 46TeachScheme, ReachJava 2010

Page 47: TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 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 13 2010 47TeachScheme, ReachJava 2010