To get your participation credit, make sure you
upload your Definitions file to turnin before you leave the lab!
(This work will not be graded---full credit just for submitting.)
If you don't finish up to the first red sentence below, finish the lab at home---otherwise you risk falling behind in the course. Also, please consider finishing the rest for fun and learning :-).
Start up DrRacket. Near the top of the interactions (lower) window, you'll see a line starting with "Language:". Make sure this reads Beginning Student. If it does not, go to language drop-down menu on the lower left of the DrRacket window and select "Beginning Student". Press the Run button, and the Language line should now show "Beginning Student".
Until otherwise announced, do your work in Beginning Student level (the error messages are more extensive and descriptive this way).
Enter (cut and paste) the following program into the definitions window:
;; babel : symbol -> string ;; produces word for hello in the given language (define (babel lang) (cond [(symbol=? lang 'spanish) "hola"] [(symbol=? lang 'french) "bonjour"] [(symbol=? lang 'pig-latin) "ellohay"]))
Using the Stepper: Put (babel 'french) at the bottom of the Definitions window and then press the Step button in the toolbar. This will bring up a separate window which lets you see how DrRacket is evaluating your program. Step through the program, making sure that you can correctly predict what the next step will be each time.
Using Check Syntax: Normally in DrRacket, constants are green, comments are orange, and identifiers are blue. Now press the Check Syntax button (or press F6). Your program has been colored so that built-in identifiers appear in navy blue, and your own identifiers appear in light blue. (To show the Interactions window again, use View menu in the toolbar.)
Move your cursor over the light blue identifiers. Notice that arrows pop up showing where each identifier is defined/used (except in define-structs). This is a tool that can help you locate errors in your programs.
Edit your program so that one of the uses of lang
is misspelled.
Press the Check Syntax button again. Notice how DrRacket highlighted
the identifier with the mistake and opened a box at the top of the
window to describe the error. Fix the misspelling then press F6, or
click on the "hide" button on the right-hand side of the message box.
Note that Check Syntax does not catch type errors. Edit the program
so that the answer returned for 'spanish
is (+ 4 'a)
. Press Check
Syntax and then Run. Now type (babel 'spanish)
, followed by a carriage return, in the Interactions window.
Working with indentation: Copy the following two lines (as is) into your definitions window:
(define-struct boa (name length food)) ;; danger-to-rodents? : boa -> boolean ;; determines whether a boa eats rats, mice, or gerbils (define (danger-to-rodents? aboa) (or (string=? (boa-food aboa) "mice")(string=? (boa-food aboa) "rats")(string=? (boa-food aboa) "gerbils")))
Edit the danger-to-rodents code so that it is indented more cleanly and more readably (readable indentation counts on the homework!). DrRacket will indent for you automatically, so you just need to decide where to put the line breaks here. (Hint: a nicely indented version will have 4 code lines, including the line with define.)
Add some spaces at the front of one line of this code to put the indentation out of alignment. Now press Tab to realign it.Keeping your code properly indented prevents many errors!
A flight simulator tracks the location and travel direction of an airplane. Assuming a 2-dimensional simulation, the location is just an x-y coordinate, and the travel velocity is a vector giving the changes in the horizontal (delta-x) and vertical (delta-y) dimensions per unit of time. (Note we are using the term "vector" as in mathematics here; do not confuse with the builtin vector type in advanced Racket, which is a linear array.)
DrRacket provides a built-in define-struct for x-y coordinates:
(define-struct posn (x y))
Do not type this into your definitions window -- it is built in!
Develop a data definition for planes that contains an image of the plane, a location (posn) and a velocity (vector).
Define a constant image1
that names the following image:
[Credit: www.free-clipart-pictures.net/airplane_clipart.html]
Create an example of a plane; use define
to name it plane1
.
Develop a function move-plane
that consumes a
plane and produces a plane. The produced plane has the same image and
velocity as the given plane, but the location coordinates are changed
by the respective amounts indicated in the velocity vector.
DrRacket includes two teachpacks used for creating interactive animations.
Using the Language > Add Teach Pack option in the toolbar, load the preinstalled image.ss
and universe.ss
from the middle (HtDP/2e) column. Then press Run.
(define WIDTH 500) (define HEIGHT 200)
Develop a function draw-plane
that consumes a
plane and produces a scene showing the plane image located at its position
coordinates. To produce a scene, use the place-image
function defined in the image.ss teachpack as follows, replacing
<image>, <x> and <y> with the correct expressions that get these
values from your plane structure:
(place-image <image> <x> <y> (empty-scene WIDTH HEIGHT))The top-left corner of the window is the origin. The x coordinate increases to the right and the y coordinate down.
Put the following lines of code at the bottom of your file:
(big-bang plane1 (on-tick move-plane) (on-draw draw-plane))When you press Run, DrRacket will pop up a window containing your plane at its starting location (make sure the location is within the
WIDTH
and HEIGHT
boundaries defined by the
constants above). You should then see your plane move across the window.
The code lines you copied are telling DrRacket to repeatedly (every
fraction of a second) call move-plane
, using draw-plane
to display the result. After each call to move-plane
,
DrRacket stores the resulting plane and uses it as the input to next call of
move-plane
.
You can stop the animation by closing the animation window. If you leave it open, you will eventually get an error from DrRacket when the plane's coordinates are overly large. Ignore this error if you see it.
Everybody should be able to finish up to this point during the lab period.
Now, we want to let the user control the plane by pressing the arrow keys to increase or decrease the angle at which the plane is coming down.
Develop a function change-velocity
that consumes
a plane and a string (the order of parameters is important) and
produces a plane. The produced plane has the same image and location
as the given plane, but possibly a different velocity vector.
If the input string is "down"
, increase the vertical component of the
vector by 0.5. If the input string is "up"
, decrease the
vertical component by 0.5. Given any other input string, just produce the
original plane unchanged (DrRacket may pass in other strings, so you
do need this else case here).
Edit the big-bang
call as follows, making sure it stays at the very
end of your Definitions window:
(big-bang plane1 (on-tick move-plane) (on-draw draw-plane) (on-key change-velocity))Now when you press Run to start the animation, your plane should move in response to presses of the up and down arrow keys.
Our current animation lets the plane fly right through the ground. Now, we want to (a) stop the animation when the plane touches the ground and (b) report either a crash or a safe landing depending on the rate of vertical descent at the time of landing.
Write a function at-ground?
that consumes a plane
and produces a boolean indicating whether the bottom of the plane
image is at the lower edge of the animation window. The built-in
function image-height
produces the height of an image in
pixels (the same units as the HEIGHT
constant already in your
file).
Edit the big-bang
call again as follows:
(big-bang plane1 (on-tick move-plane) (on-draw draw-plane) (on-key change-velocity) (stop-when at-ground?))
If you are doing very well, you will finish up to this point
The rest of these steps are here for those going extremely fast or who want to see how to finish the animation.
Develop a function steep-slope?
that consumes a
plane and determines whether the plane is descending more than 3 times
faster in the vertical direction than in the horizontal.
Edit your draw-plane
function to put a large text
message in the animation window when the plane touches the ground.
The message should be different depending on whether the plane landed
steeply or not (such as "CRASH!" versus "Safe landing"). You can
produce a scene with text using the following:
(place-image (text <string> <font-size> <color>) <x> <y> (empty-scene WIDTH HEIGHT))For
<color>
you can provide a string such as "red"
.
Obviously, there's much more you can do with this, such as stopping the animation when the plane flies out of the window, or wrapping the plane around the left/right edges, or giving the user more controls.
For more about using the universe.ss teachpack for graphical simulations, see the free online book How to Design Worlds.