Start up DrRacket. Enter (cut and paste) the following program into the definitions window:
(define (greeting lang) (if (symbol=? lang 'pig-latin) 'ellohay 'hello))
This program uses symbols, which are a lot like strings, except that symbols can't have any embedded spaces in them. Here is some more information on the differences between symbols and strings.
Run the program by typing (greeting 'english) at the prompt in the interactions window. Notice which parts of the code are highlighted -- those have not yet been tested.
Using the Stepper: Put (greeting 'english) in the definitions window. Press the "Step" button. 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 the HelpDesk (DrRacket's, not WPI's): Assume you
wanted to edit the greeting function to use strings instead of
symbols, but forget how to check whether two strings are the same.
Under the "Help" menu, select "Help Desk". DrRacket will open a new
browser window. At the top of the page you'll now see a search box.
Enter
string
into that box and hit enter. From the matches that appear,
choose "string" provided from lang/htdp-beginner. You will get a
display of the built-in functions on strings.
Make it a habit to try to answer your own questions using the Help Desk before asking a classmate or the course staff.
Working with indentation: Copy the following two functions (as they are) into your definitions window:
;; celsius->fahrenheit : Number -> Number ;; consumes temperature in celsius and produces temp in fahrenheit (define (celsius->fahrenheit temp) (+ (* 9/5 temp) 32)) ;; recommend-sport : Number Symbol -> String ;; consumes temp in celsius and what kind of activity person likes and ;; produces a recommendation for an activity (define (recommend-sport temp activity-type) (if (and (> (celsius->fahrenheit temp) 0) (< (celsius->fahrenheit temp) 32)) (if (symbol=? activity-type 'motorized) "snow mobiling" "ice-fishing") (if (symbol=? activity-type 'active) "badminton" "tv")))
Edit the code so that it is indented more cleanly and more readably (readable indentation counts on the homework!). Remember that DrRacket will indent for you automatically, so you just need to decide where to put the line breaks here.
Introduce various syntax errors into your program (remove a paren, put in too many parens, misspell a word, leave one of the answers out of an if statement, etc) and experiment with DrRacket's error messages.
Everybody should be able to finish up to this point during lab. Do as many of the remaining exercises as you can before you leave lab today. Finish all exercises that you don't finish during the lab on your own time (we won't always ask you to do this, but this last set of problems introduces you to writing animation programs in DrRacket... we'll continue this animation exercise in Lab 2.)
In order to use the animation features of DrRacket, you need to use the library functions from a library called universe. Add these lines to the beginning of your definitions file:
(require 2htdp/universe) (require 2htdp/image)
The essence of a soccer game is a ball on a field. For the ball, either copy/paste the ball image into the DrRacket definitions window or download the image to your computer and import it into your file using the "Insert image" option under the "Insert" menu.
(from www.hscripts.com)
Use define
to create a constant name for this image.
Using the following expression as a template, experiment in the Interactions window with several expressions that place a ball into an empty animation scene (creating a new scene). The numbers 300 and 200 represent the width and height of the window that will be drawn. You should replace the terms in italics with appropriate expressions.
(place-image ball-image x-position y-position (empty-scene 300 200))
Write a function place-ball-x
that consumes a number
representing the x-coordinate of the soccer ball and produces a scene with the ball drawn at that x-coordinate and some fixed y-coordinate (create a constant for the y-coordinate and choose your own value for it).
Write a function update-ball-x
that consumes a number
representing an x-coordinate and produces a new x-coordinate (the new
x-coordinate will be the desired position of the ball in the next frame of the animation). Moving the coordinate by a small number (3 or 4) works well.
You are now ready to create your first animation. Place the following lines in your Definition window:
(big-bang 0 (on-tick update-ball-x) (to-draw place-ball-x))(The 0 represents the starting x-coordinate of the ball. On every "tick" of the animation clock, your
update-ball-x
will execute, changing the
x-coordinate of the ball. Your place-ball-x
function will then
draw the ball at the new coordinate.)
When you hit run, you should see the ball move across the screen.
Write a function at-right-edge?
that consumes an x-coordinate and determines whether the right edge of the ball is touching (or beyond) the right edge of the screen. Introducing a constant for the screen width (and updating old uses of this value) would be a good idea at this point.
Add the line
(stop-when at-right-edge?)to the commands that run the animation. When you hit run, the ball should stop moving when it reaches the right edge of the screen (indicating that the animation has stopped).
(from www.fotosearch.com)
Write a function place-goalie-y
that consumes a y-coordinate and produces a scene with the goalie drawn at that y-coordinate and some fixed x-coordinate (create a constant for the x-coordinate and choose your own value for it).
Write a function goalie-react
that consumes the goalie's current y-coordinate and a string (which represents which key was pressed by the
user) and produces a new y-coordinate for the goalie. If the given key value
is "up" subtract 3 from the current y-coordinate. If it is "down", add 3 to the current y-coordinate. Otherwise, produce the same coordinate. Use the built-in function key=?
to compare the input symbol to the possible key values.
You are now ready to create your second animation. Place the following lines in your Definitions window:
(big-bang 120 (on-key goalie-react) (to-draw place-goalie-y))The 120 represents the starting y-coordinate of the goalie. When you hit run, you should see the goalie move when the user presses the up and down arrow keys.
Next week, we'll combine the ball and goalie animations into one program.