Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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 20102TeachScheme, ReachJava 2010

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

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

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

6 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 20106TeachScheme, ReachJava 2010

7 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 20107TeachScheme, ReachJava 2010

8 Writing functions on posns Almost any function that takes in a posn looks like (define (function-on-posn where) ; where posn ) (the "inventory" step of the recipe) July 13 20108TeachScheme, ReachJava 2010

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

10 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 201010TeachScheme, ReachJava 2010

11 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 201011TeachScheme, ReachJava 2010

12 Exercises on posns Write 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 201012TeachScheme, ReachJava 2010

13 Optional exercises on posns Write 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 201013TeachScheme, ReachJava 2010

14 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 201014TeachScheme, ReachJava 2010

15 Model & handlers Model is a posn representing center of picture We'll need a redraw handler ; calendar-at-posn : posn -> image and a key handler ; handle-key : posn key -> posn July 13 201015TeachScheme, ReachJava 2010

16 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 201016TeachScheme, ReachJava 2010

17 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 201017TeachScheme, ReachJava 2010

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

19 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 201019TeachScheme, ReachJava 2010

20 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 201020TeachScheme, ReachJava 2010

21 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 201021TeachScheme, ReachJava 2010

22 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 201022TeachScheme, ReachJava 2010

23 Structs posn 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 2010TeachScheme, ReachJava 201023

24 Structs What if you need to "package up" something other than two or three numbers? July 13 2010TeachScheme, ReachJava 201024

25 Defining our own structs 1)Choose a name for the new data type 2)Choose names and types for each of its parts 3)Write a define-struct (next slide) 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 201025TeachScheme, ReachJava 2010

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

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

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

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

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

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

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

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

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

35 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 201035TeachScheme, ReachJava 2010

36 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 201036TeachScheme, ReachJava 2010

37 Defining another animal Define 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 201037TeachScheme, ReachJava 2010

38 Animations with user-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 2010TeachScheme, ReachJava 201038

39 Definition by choices New 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 201039TeachScheme, ReachJava 2010

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

41 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 201041TeachScheme, ReachJava 2010

42 Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) ) July 13 201042TeachScheme, ReachJava 2010

43 Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) (cond [(dog? the-animal) ] [(fish? the-animal) ])) July 13 201043TeachScheme, ReachJava 2010

44 Function template for animals Almost 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 201044TeachScheme, ReachJava 2010

45 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 201045TeachScheme, ReachJava 2010

46 Function template for animals Again, 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 201046TeachScheme, ReachJava 2010

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

48 Another exercise Write 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 201048TeachScheme, ReachJava 2010

49 Lab exercise Open shapes-lab.scm (in Examples folder) Do the exercises in it July 13 201049TeachScheme, ReachJava 2010

50 Are we done yet? Fill out end-of-day survey www.adelphi.edu/sbloch/class/tsrj/daily.html Eat Sleep Come back for another day July 13 201050TeachScheme, ReachJava 2010


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

Similar presentations


Ads by Google