Start up DrScheme. Near the top of the interactions (lower) window, you'll see a line starting with the word "Language". Make sure this reads Beginning Student. If it does not, go to "Choose Language" under the "Language" menu and set the language to beginner level (under the "How to Design Programs" category). Press the Run button, and the Language line should now read "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) in the definitions window. Press the "Step" button. This will bring up a separate window which lets you see how DrScheme 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: Edit your program so that one of the uses of "lang" is misspelled. Press the "Check Syntax" button. Your program has been colored in black, green, red, and blue. Built-in operators appear in black. User-defined identifiers appear in blue. Constants appear in green. Identifiers that DrScheme doesn't recognize appear in red. Your misspelled identifier should appear in red.
Move your cursor over the blue symbols. Notice that blue arrows pop up showing where each identifier is defined/used (except in define-structs). This is another tool that can help you locate errors in your programs.
Note that Check Syntax does not look for type errors. Edit the program so that the answer returned for 'spanish is (+ 4 'a). Check Syntax, then run (babel 'spanish).
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!). DrScheme will indent for you automatically, so you just need to decide where to put the line breaks here.
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 direction is a vector giving the changes in the horizontal and vertical dimensions per unit of time.
DrScheme provides a built-in define-struct for 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 direction (vector). Create your own data-definition for the vector.
Create an example of a plane; use define to name it plane1. For the image, download the following gif and insert it into your file using the "insert image" option under the "special" menu.
(image from www.free-clipart-pictures.net/airplane_clipart.html)
Develop a function
move-plane that consumes a
plane and produces a plane. The produced plane has the same image and
direction as the given plane, but the location coordinates are changed
by the respective amounts indicated in the direction vector.
DrScheme includes a teachpack for creating interactive animations.
world.ss teachpack (under the Languages menu,
then press Run). Put the following two definitions at the top of your
(define WIDTH 500) (define HEIGHT 200)
Develop a function
draw-plane that consumes a
plane and produces a scene showing the plane image at its
coordinates. To produce a scene, use the
function defined in the world.ss teachpack as follows, replacing
[plane-image], [at-x] and [at-y] with expressions that get these
values from your plane structure:
(place-image [plane image] [at-x] [at-y] (empty-scene WIDTH HEIGHT))The top-left corner is position 0,0. Larger coordinates move down and to the right.
Put the following lines of code at the bottom of your file:
(big-bang WIDTH HEIGHT (/ 1.0 28) plane1) ;; plane1 is the name used in problem 2 (on-tick-event move-plane) (on-redraw draw-plane)When you press Run, DrScheme will pop up a window containing your plane at its original location (make sure the location is within the WIDTH and HEIGHT boundaries defined by the constants above). You should see your plane move across the window. The code lines you copied are telling DrScheme to call
move-planeevery (/ 1 28) seconds, using draw-plane to display the result. After each call to
move-plane, DrScheme stores the resulting plane and uses it as the input to
move-planeat the next time.
You can stop the animation by closing the animation window. If you leave it open, you will eventually get an error from DrScheme 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 (assuming 10 minutes exploring DrScheme, 10 minutes on probs 1-2, 10 minutes on problem 3, 10 minutes on problem, and a couple of minutes to run the animation for problem 5)
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-plane-vec that consumes
a plane and a symbol (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 new direction vector.
If the input symbol is
'down, increase the vertical
vector by .5. If the input symbol is
'up, decrease the
vertical vector by .5. On any other symbol, just produce the
original plane unchanged (DrScheme may pass in other symbols, so you
do need the else case here).
Add the following line to the bottom of your file:
(on-key-event change-plane-vec)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
image-height produces the height of an image in
pixels (the same units as the HEIGHT constant already in your
Add the following line to your file to get the animation to stop when the plane lands:
If you are doing very well, you will finish up to this point
The rest of these 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.
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]) [at-x] [at-y] (empty-scene WIDTH HEIGHT))
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. More information about working with key and mouse events is in the documentation for world.ss (just look up world.ss in the helpdesk).