NOTE: DrRacket saves the work done in the Definitions window only.
Start up DrRacket. Enter the following program into the definitions window:
(define (babel lang) (cond [(symbol=? lang 'spanish) 'hola] [(symbol=? lang 'french) 'bonjour] [(symbol=? lang 'pig-latin) 'ellohay]))
(By the way, actually typing in the code rather than cutting/pasting helps you to internalize the syntax patterns.)
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.
This program also uses the cond statement. cond
will be explained in detail in the videos you'll be watching on Thursday night. For the time being, you need to know that cond
is similar to if
, but it allows you to specify more than two alternatives. So in the example above, if the value provided for lang
is 'spanish
, then the value of the cond
expression will be 'hola
.
Run the program by typing (babel 'french) at the prompt in the interactions window. Notice which parts of the code are highlighted -- those parts have not yet been tested.
Run the program on the language 'german (type (babel 'german)). Edit the program to translate to german as well (the translation is 'hallo). Run the program on 'german again (Note: you can hit the escape key followed by p to have DrRacket scroll backwards through the previous expressions that you typed at the prompt). Notice that DrRacket does not register your edits until you press Run (you may wish to try running the program before and after you press Run to see what happens).
Save your program using the "Save" option in the file menu.
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 DrRacket is evaluating your program. Step through the program, and see if you can correctly predict what the next step will be each time.
Using Check Syntax: Press the "Check Syntax" button. Your program appears in mutiple colors, one for built-in operators, one for programmer-defined operators and identifiers, and one for data (strings, numbers, etc). Edit your program so that one of the uses of "lang" is misspelled. Press the "Check Syntax" button again. You should get an error message at the top of the DrScheme window. Move your cursor over the identifiers. Notice that arrows pop up showing where each identifier is defined/used. This is another tool that can help you locate errors in your programs.
Using the HelpDesk (DrRacket's, not WPI's): Assume you
wanted to edit the babel 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. Scroll down to get an idea of the kinds of functions that are available for operating on strings.
Scroll back to the top of the page, and this time, enter overlay
into the search box. From the matches that appear, choose "overlay" provided from
2htdp/image. You'll see a description of how to use the overlay function.
Note that we will be using the image functions available with 2htdp (not
the functions available from
htdp). The two sets of functions are different.
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 the 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 outdoor activity (define (recommend-sport temp activity-type) (cond [(and (> (celsius->fahrenheit temp) 0) (< (celsius->fahrenheit temp) 32)) (cond [(symbol=? activity-type 'motorized) "snow mobiling"] [(symbol=? activity-type 'feet) "skiing"] [else "ice-fishing"])] [(and (> (celsius->fahrenheit temp) 50) (< (celsius->fahrenheit temp) 80)) "hiking"] [else (cond [(symbol=? activity-type 'motorized) "stationary bike"] [(symbol=? activity-type 'feet) "ping-pong indoors"] [else "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 the answer out of a cond clause, etc) and experiment with DrRacket's error messages.