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

50
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010

Transcript of TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.

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

TeachScheme, ReachJava

Adelphi University

Tuesday afternoon

July 13, 2010

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

Recall left/right arrow animation

• Could obviously have picture move up or down, rather than left or right

• Could have circle of radius that increases or decreases

• etc.• How about a picture that moves up, down,

left, or right?Problem: need to "remember" both x and y coordinates

July 13 2010 2TeachScheme, ReachJava 2010

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

Definition by parts

Sometimes several related data need to be passed around together, e.g.

• name, SSN, and salary of employee• x and y coordinates of 2-D point• name, length, and favorite food of boa-constrictor

Scheme allows you to define a new data type with several "parts"

July 13 2010 3TeachScheme, ReachJava 2010

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

A predefined struct

There's a built-in type named posn, which has two parts x and y.

Built-in functions

; make-posn : number(x) number(y) -> posn

; posn-x : posn -> number

; posn-y : posn -> number

; posn? : anything -> boolean

July 13 2010 4TeachScheme, ReachJava 2010

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

For those who know Java…

• make-posn is a constructor.

• posn-x and posn-y are getter methods.

• posn? is like instanceof Posn.

• There are also setter methods, but we'll get to them later.

July 13 2010 5TeachScheme, ReachJava 2010

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

Using posns

"Examples of the posn type:"

(make-posn 3 5)

(make-posn 12 -7)

(define here (make-posn 14 9))

(check-expect (posn-x here) 14)

(check-expect (posn-y here) 9)July 13 2010 6TeachScheme, ReachJava 2010

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

Writing functions on posns

Almost any function that takes in a posn looks like(define (function-on-posn where)

)

(the "skeleton" step of the recipe)

July 13 2010 7TeachScheme, ReachJava 2010

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

Writing functions on posnsAlmost any function that takes in a posn looks like(define (function-on-posn where)

; where posn

)

(the "inventory" step of the recipe)

July 13 2010 8TeachScheme, ReachJava 2010

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

Writing functions on posns

Almost any function that takes in a posn looks like(define (function-on-posn where)

; where posn; (posn-x where) number; (posn-y where) number

)

(the "inventory" step of the recipe, continued)

July 13 2010 9TeachScheme, ReachJava 2010

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

Using posns

Write a function right-of-100? which takes in a posn and tells whether its x coordinate is greater than 100

Work this out together

July 13 2010 10TeachScheme, ReachJava 2010

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

Creating posns

Write a function diagonal-posn which takes in a number and produces a posn whose x and y coordinates are both that number

Work this out together

July 13 2010 11TeachScheme, ReachJava 2010

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

Exercises on posnsWrite a function above-main-diagonal? which takes in a

posn and tells whether it is above the diagonal line "x=y" (note that positive x is to the right, and positive y is down, as usual in computer graphics)

Write a function posn=? which takes in two posns and tells whether they have the same x coordinate and the same y coordinate

Write a function swap-x-y which takes in a posn and returns a posn whose x coordinate is the y coordinate of the given posn, and vice versa.

July 13 2010 12TeachScheme, ReachJava 2010

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

Optional exercises on posnsWrite a function distance which takes in two posns and returns their

Euclidean distance, which is given by the formula sqrt((x1-x2)2 + (y1-y2)2)

Write a function scale-posn which takes in a posn and a number and produces a posn by multiplying each coordinate of the given posn by the number

Write a function add-posns which takes in two posns and produces a

posn whose x coordinate is the sum of their x coordinates, and likewise for the y coordinates

July 13 2010 13TeachScheme, ReachJava 2010

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

Animations with posn models

Write an animation of a picture that moves up, down, left, and right in response to the corresponding arrow keys

July 13 2010 14TeachScheme, ReachJava 2010

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

Model & handlers

• Model is a posn representing center of picture

• We'll need a redraw handler; calendar-at-posn : posn -> imageand a key handler; handle-key : posn key -> posn

July 13 2010 15TeachScheme, ReachJava 2010

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

Draw handler; calendar-at-posn : posn -> image(check-expect (calendar-at-posn (make-posn 27 104)) (place-

image calendar 27 104 BACKGROUND))(check-expect (calendar-at-posn (make-posn 215 6)) (place-

image calendar 215 6 BACKGROUND))

(define (calendar-at-posn where); where posn; (posn-x where) number; (posn-y where) number(place-image calendar (posn-x where) (posn-y where) BACKGROUND))

July 13 2010 16TeachScheme, ReachJava 2010

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

Key handler: contract & examples

; handle-key : posn key -> posn(check-expect (handle-key (make-posn 12 47) "v")

(make-posn 12 47))(check-expect (handle-key (make-posn 12 47) "up")

(make-posn 12 46))(check-expect (handle-key (make-posn 12 47) "down")

(make-posn 12 48))(check-expect (handle-key (make-posn 12 47) "left")

(make-posn 11 47))(check-expect (handle-key (make-posn 12 47) "right")

(make-posn 13 47))(check-expect (handle-key (make-posn 12 47) "home")

(make-posn 12 47))

July 13 2010 17TeachScheme, ReachJava 2010

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

Key handler: skeleton & inventory

; handle-key : posn key -> posn(define (handle-key old-place key)

(cond [(key=? key "left") …]

[(key=? key "right") …]

[(key=? key "up") …]

[(key=? key "down") …]

[else old-place]))

July 13 2010 18TeachScheme, ReachJava 2010

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

Key handler: skeleton & inventory

; handle-key : posn key -> posn(define (handle-key old-place key)

(cond [(key=? key "left") (add-posns old-place (make-posn -1 0))]

[(key=? key "right") (add-posns old-place (make-posn 1 0))]

[(key=? key "up") (add-posns old-place (make-posn 0 -1))]

[(key=? key "down") (add-posns old-place (make-posn 0 1))]

[else old-place]))

July 13 2010 19TeachScheme, ReachJava 2010

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

Or more briefly…

; handle-key : posn char-or-symbol -> posn(define (handle-key old-place key)

(add-posns old-place (cond [(symbol=? key "left") (make-posn -1 0))] [(symbol=? key "right") (make-posn 1 0))

[(symbol=? key "up") (make-posn 0 -1)) [(symbol=? key "down") (make-posn 0 1)) [else (make-posn 0 0)]))

July 13 2010 20TeachScheme, ReachJava 2010

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

Running the animation(big-bang

(make-posn (/ WIDTH 2) (/ HEIGHT 2))(check-with posn?)(on-draw calendar-at-posn)(on-key handle-key))

July 13 2010 21TeachScheme, ReachJava 2010

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

Exercises

• Write an animation of a picture that, every second, teleports to a random location within the window, but ignores key & mouse events

• Write an animation of a picture that moves randomly up, down, left, or right by a pixel each 0.1 second

July 13 2010 22TeachScheme, ReachJava 2010

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

Structsposn is an example of a "structure type": it's made of

two parts (x and y), each of which is a number.

Another pre-defined structure type is color; it has three parts (red, green, and blue), each of which is a number 0-255.

(define my-color (make-color 50 200 200))

(triangle 50 "solid" my-color)

(check-expect (color-red my-color) 50)

July 13 2010 TeachScheme, ReachJava 2010 23

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

Structs

What if you need to "package up" something other than two or three numbers?

July 13 2010 TeachScheme, ReachJava 2010 24

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

Defining our own structs

1) Choose a name for the new data type2) Choose names and types for each of its parts3) Write a define-struct (next slide) to tell

Scheme about it4) Write contracts for the constructor and accessor

methods5) Write examples of the new data type6) Write a function template for the new data type7) Start writing functions on the new data type

July 13 2010 25TeachScheme, ReachJava 2010

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

Syntax Rule: Defining a Struct

To define a structure named foo with fields snark and boojum,

(define-struct foo (snark boojum))

This defines a new data type foo and several functions:

; make-foo : snark-type boojum-type -> foo

; foo-snark : foo -> snark-type

; foo-boojum : foo -> boojum-type

; foo? : anything -> boolean

July 13 2010 26TeachScheme, ReachJava 2010

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

Example: posn(if it weren't pre-defined)

; A posn consists of two numbers (x and y)

(define-struct posn (x y))

; make-posn : number number -> posn; posn-x : posn -> number; posn-y : posn -> number; posn? : anything -> boolean

"Examples of the posn data type:"(make-posn 3 5)(make-posn 12 -7)(define here (make-posn 14 9))(check-expect (posn-x here) 14)(check-expect (posn-y here) 9)July 13 2010 27TeachScheme, ReachJava 2010

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

Real example: dogs

; A dog has a string(name), number(age), number(weight), and a boolean(asleep?)

(define-struct dog (name age weight asleep?))

; make-dog : string num num boolean -> dog; dog-name : dog -> string; dog-age : dog -> number; dog-weight : dog -> number; dog-asleep? : dog -> boolean

July 13 2010 28TeachScheme, ReachJava 2010

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

Real example: dogs

"Examples of the dog data type:"(make-dog "Ludo" 6 78 true)(make-dog "Thibaut" 4 74 false)

(define this-dog (make-dog "Rover" 8 45 false))

(check-expect (dog-name this-dog) "Rover")(check-expect (dog-age this-dog) 8)(check-expect (dog-weight this-dog) 45)(check-expect (dog-asleep? this-dog) false)

July 13 2010 29TeachScheme, ReachJava 2010

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

Type-based coding patterns

Almost any function that takes in a dog looks like

(define (function-on-dog the-dog)

)

(the "skeleton" step of the recipe)

July 13 2010 30TeachScheme, ReachJava 2010

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

Type-based coding patterns

Almost any function that takes in a dog looks like

(define (function-on-dog the-dog)

; the-dog a dog

)

(the "inventory" step of the recipe)

July 13 2010 31TeachScheme, ReachJava 2010

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

Type-based coding patterns

Almost any function that takes in a dog looks like

(define (function-on-dog the-dog)

; the-dog a dog

; (dog-name the-dog) a string

; (dog-age the-dog) a number

; (dog-weight the-dog) a number

; (dog-asleep? the-dog) a boolean

)

(the "inventory" step of the recipe, continued)

July 13 2010 32TeachScheme, ReachJava 2010

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

Type-based coding patterns

#|(define (function-on-dog the-dog) ; the-dog a dog ; (dog-name the-dog) a string ; (dog-age the-dog) a number ; (dog-weight the-dog) a number ; (dog-asleep? the-dog) a boolean )|#This header-plus-inventory, commented out, can be used as a template,

copying and pasting it as a starting point for any function that takes in a dog.

July 13 2010 33TeachScheme, ReachJava 2010

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

A function on dogs

Write a function fits-in-lap? which takes in a dog and the weight capacity of a lap, and tells whether the dog will fit in the lap in question

Work this out together

July 13 2010 34TeachScheme, ReachJava 2010

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

Your turn

Write a function movable? which takes in a dog. If the dog is awake, it's movable. If the dog is asleep but under 20 pounds, it's movable. Otherwise it's not. (Hint: it's shorter and simpler if you don't use a cond.)

Write a function birthday which takes in a dog and returns a dog with the same name, weight, and sleep status, but one year older.

July 13 2010 35TeachScheme, ReachJava 2010

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

Inventors and Factories

define-struct is like an inventor.Specifies once what's in a particular model of cell phone, but doesn't actually manufacture them. Then invents something else, and doesn't manufacture them either….

make-posn is like a cell-phone factory.Each factory "knows" how to build one kind of thing (cell phones, posns, dogs, etc.)Factory doesn't exist until the thing is invented.Can be used over and over to build many cell-phones/posns/dogs/whatever.

July 13 2010 36TeachScheme, ReachJava 2010

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

Defining another animalDefine a data structure fish which has a string(color), a

number(weight) and a boolean(salt-water?).

Remember the steps:

1) Choose a name for the new data type 2) Choose names and types for each of its parts 3) Write a define-struct to tell Scheme about it

4) Write contracts for the constructor and accessor methods

5) Write examples of the new data type

6) Write a function template for the new data type

7) Start writing functions on the new data type

July 13 2010 37TeachScheme, ReachJava 2010

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

Animations withuser-defined structures

Worked exercise 21.6.1 in textbook

Modifications: exercises 21.6.2 and 21.6.3

If you finish these, look at exercises 21.7.5-10.

July 13 2010 TeachScheme, ReachJava 2010 38

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

Definition by choicesNew data type: animal

; An animal is either a dog or a fish.

Scheme doesn't enforce this; it's up to the programmer.

July 13 2010 39TeachScheme, ReachJava 2010

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

A function on animals

Write a function fits-in-crate? which takes in an animal (either a dog or a fish) and the weight capacity of a crate, and tells whether the animal can be shipped in that crate.

Hint: fish are never shipped in crates, regardless of weight.

July 13 2010 40TeachScheme, ReachJava 2010

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

How to write this?

The input type, animal, is one of two sub-categories (dog or fish), so…

• we need at least two test cases, one of each type (in fact, we'll need three dogs — under, over, and borderline — and at least one fish)

• the function body will probably be a cond with two cases, with questions "dog?" and "fish?"

July 13 2010 41TeachScheme, ReachJava 2010

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

Function template for animalsAlmost any function on animals will look like(define (function-on-animal the-animal)

)

July 13 2010 42TeachScheme, ReachJava 2010

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

Function template for animalsAlmost any function on animals will look like(define (function-on-animal the-animal) (cond [(dog? the-animal)

] [(fish? the-animal)

]))

July 13 2010 43TeachScheme, ReachJava 2010

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

Function template for animalsAlmost any function on animals will look like(define (function-on-animal the-animal) (cond [(dog? the-animal) ; the-animal a dog

] [(fish? the-animal) ; the-animal a fish

]))

July 13 2010 44TeachScheme, ReachJava 2010

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

Function template for animals#|(define (function-on-animal the-animal) (cond [(dog? the-animal) ; the-animal a dog ; (dog-name the-animal) a string ; (dog-age the-animal) a number ; (dog-weight the-animal) a number ; (dog-asleep? the-animal) a boolean ] [(fish? the-animal) ; the-animal a fish ; (fish-color the-animal) a string ; (fish-weight the-animal) a number ; (fish-salt-water? the-animal) a boolean ]))|#July 13 2010 45TeachScheme, ReachJava 2010

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

Function template for animalsAgain, we can copy and paste this as a starting point

for any function on animals.In practice, much of it is irrelevant to any given

function, so we can delete those lines.

July 13 2010 46TeachScheme, ReachJava 2010

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

My answer to fits-in-crate?; fits-in-crate? : animal number -> boolean(define (fits-in-crate? the-animal max-weight) ; max-weight a number (cond [(dog? the-animal) ; (dog-weight the-animal) a number (<= (dog-weight the-animal) max-weight) ] [(fish? the-animal) false ]))"Examples of fits-in-crate?:"(check-expect (fits-in-crate? (make-dog "Bob" 3 58 true) 50) false)(check-expect (fits-in-crate? (make-dog "Dave" 2 65 true) 65) true) ; borderline(check-expect (fits-in-crate? (make-dog "Eddie" 7 35 false) 50) true)(check-expect (fits-in-crate? (make-fish "orange" 0.03 false) 5) false)

July 13 2010 47TeachScheme, ReachJava 2010

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

Another exerciseWrite a function underweight? which takes in an animal and

tells whether or not it's underweight — which means under 30 pounds for a dog, and under 0.1 pounds for a fish.

July 13 2010 48TeachScheme, ReachJava 2010

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

Lab exercise

• Open shapes-lab.scm (in Examples folder)• Do the exercises in it

July 13 2010 49TeachScheme, ReachJava 2010

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

Are we done yet?

• Fill out end-of-day surveywww.adelphi.edu/sbloch/class/tsrj/daily.html

• Eat• Sleep• Come back for another day

July 13 2010 50TeachScheme, ReachJava 2010