1 / 50

TeachScheme, ReachJava

Learn to create an animation of a picture that can move up, down, left, and right in response to arrow keys using posns in TeachScheme and ReachJava programming languages.

jodieb
Download Presentation

TeachScheme, ReachJava

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  5. For those who know Java… • make-posnis a constructor. • posn-xandposn-y are getter methods. • posn? is like instanceof Posn. • There are also setter methods, but we'll get to them later. TeachScheme, 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) TeachScheme, 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) TeachScheme, 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) TeachScheme, 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) TeachScheme, 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 TeachScheme, 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 TeachScheme, 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. TeachScheme, 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 TeachScheme, 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 TeachScheme, ReachJava 2010

  15. 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 TeachScheme, 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)) TeachScheme, 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)) TeachScheme, 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])) TeachScheme, 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])) TeachScheme, 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)])) TeachScheme, 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)) TeachScheme, 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 TeachScheme, 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) TeachScheme, ReachJava 2010

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

  25. Defining our own structs • Choose a name for the new data type • Choose names and types for each of its parts • Write a define-struct (next slide) to tell Scheme about it • Write contracts for the constructor and accessor methods • Write examples of the new data type • Write a function template for the new data type • Start writing functions on the new data type TeachScheme, 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 TeachScheme, 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) TeachScheme, 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 TeachScheme, 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) TeachScheme, 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) TeachScheme, 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) TeachScheme, 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) TeachScheme, 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. TeachScheme, 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 TeachScheme, 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. TeachScheme, 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. TeachScheme, 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: • Choose a name for the new data type  • Choose names and types for each of its parts  • Write a define-struct to tell Scheme about it • Write contracts for the constructor and accessor methods • Write examples of the new data type • Write a function template for the new data type • Start writing functions on the new data type TeachScheme, ReachJava 2010

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

  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. TeachScheme, 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. TeachScheme, 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?" TeachScheme, ReachJava 2010

  42. Function template for animals Almost any function on animals will look like (define (function-on-animal the-animal) ) TeachScheme, 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) ])) TeachScheme, 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 ])) TeachScheme, 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 ])) |# TeachScheme, 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. TeachScheme, 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) TeachScheme, 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. TeachScheme, ReachJava 2010

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

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

More Related