Lab 3 Structure types; World programs with structures
Purpose: to review structure type definitions, and to practice their use, especially in the context of World programs.
Posns
Consider this data design for a Position:
; A Position is a (make-posn Number Number) ; Int.: the x and y coordinates of a point in 2d space (define pos-0 (make-posn 0 0)) (define pos-1 (make-posn 1 -3)) (define pos-2 (make-posn 1.5 pi)) (define (position-templ p) (... (posn-x p) ... (posn-y p) ...))
Exercise 1 Can you explain the difference between a Position and a (make-posn Number Number)?
Exercise 2 Design the function pos-adder which takes two Positions and produces a single Position whose x-coordinate is the sum of the two x-coordinates and whose y-coordinate is the sum of the two y-coordinates. Be sure to follow all the steps of the design recipe for functions.
Exercise 3 Below is a data definition for a book. What is the problem with this data definition?
Custom Structures
Exercise 4 The Franklin Park Zoo wants to keep better track of their animals but they need your help! Each animal at the zoo has a name, a species, an indicator of whether they are kept outside or inside, and a diet which can be either herbivorous, carnivorous, or omnivorous. Design appropriate data definitions to describe an animal at the zoo. Be sure to follow all the steps of the design recipe for data.
Consider the following data definitions:
(define-struct address [num st city us-state zip]) ; An Address is a (make-address Nat String String String Nat) where ; - num is the number of the building on the street ; - st is the name of the street ; - city is the city the building is in ; - us-state is the state the city is in, and ; - zip is the zipcode of the building (define-struct student [first last nuid local perm]) ; An NEUStudent is a ; (make-student String String PositiveNumber Address Address) where ; - first is the student's first name ; - last is the student's last name ; - nuid is the student's NUID # ; - local is the student's local address, and ; - perm is the student's permanent address
Exercise 5 Complete the data design recipe for the above data definitions.
Exercise 6 Design the function student-email which takes an NEUStudent and produces a string representing that student’s email address. For simplicity we will say that a student’s email address has the form,
"<lastname>.<first-initial>@husky.neu.edu"
The last name and the first-name initial in the email address are all lowercase.
Exercise 7 Design the function new-zipcode which takes an NEUStudent and a number, representing the new zip code of the person after a move, and produces a student with all of the same information except that their permanent address will have the given zip code. Be sure to follow the template!
A Simple Game
In the remaining time we will work on designing a small program which allows you to move a dot by clicking the mouse. The program should display a black background with a white dot at some given initial location and then when the user clicks the dot should appear at the mouse’s location instead. To do this, we will follow the same steps for designing programs as we did in the previous lab.
Step 1: What stays the same?
Exercise 8 Define some constants to describe what stays the same throughout the game. What images will you need in order to draw the program?
Step 2: What changes?
The only thing that changes in this program is the location of the dot. How can we best represent this? Think about the types of data you know and choose one that represents a location in 2d space. This will be your world state.
Step 3: Which handlers do we need?
Exercise 9 Write the signature and purpose statements for the handler functions you will need. Recall that big-bang always requires a to-draw clause. Which other clauses will you need based on the program description? If you are having trouble take a look at the documentation, and if you are still confused, ask a tutor or TA for assistance.
Step 4: Main Function
Now that we’ve decided on our handler functions, we can put together our main function which will call big-bang. Recall that we write this function first because in this course we follow a top-down programming approach. That means if function main-func uses function helper-func, we design function main-func first, then function helper-func.
Exercise 10 Design the main function dot-clicker which, given an initial position for the dot, starts up the big-bang program.
Step 5: Design your handlers
Exercise 11 Design the handlers you decided on in step 3.
Give your program a try! Recall that the dot should only change positions when the mouse is clicked, and not every time the mouse moves. Good luck!