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 Execute 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:
(define (babel lang) (cond [(string=? lang "spanish") "hola"] [(string=? lang "french") "bonjour"] [(string=? lang "pig-latin") "ellohay"]))
Run the program by typing (babel "french") at the prompt in the interactions window. Notice which parts of the code are still in red -- those have not yet been tested.
Run the program on the language "german". Edit the program to translate to german as well (the translation is "hallo"). Run the program on "german" again (Note: you can hit "escape" followed by "p" to have DrScheme scroll backwards through the previous expressions that you typed at the prompt). Notice that DrScheme does not register your edits until you press Execute (you may wish to try running the program before and after you press Execute to see what happens).
Save your program using the "Save Definitions" 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 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: 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 (DrScheme's, not WPI's): Assume you were writing the babel function but forgot how to check whether two strings are the same. Under the "Help" menu, select "Help Desk". At the bottom of the window that pops up, you'll see a box labelled "find docs for". Enter string into that box and hit search (or enter). You will get a display of the built-in functions on strings. Try the same search on image to get a list of the image operators. Click on the definition of overlay/xy and you will get the contract and purpose for overlay/xy.
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 string -> 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 [(string=? activity-type "motorized") "snow mobiling"] [(string=? activity-type "feet") "skiing"] [else "ice-fishing"])] [(and (> (celsius->fahrenheit temp) 50) (< (celsius->fahrenheit temp) 80)) "hiking"] [else (cond [(string=? activity-type "motorized") "stationary bike"] [(string=? 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 DrScheme 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 DrScheme's error messages.
A simple calculation of the risk of forest fires is based on the relative humidity and current wind speed. Fire risk is high if the relative humidity is 7% or lower and winds are greater than 20 mph. Fire risk is low if relative humidity is above 20% or winds are less than 5mph. In all other cases, fire risk is medium.
Write a program
fire-risk that consumes the
relative humidity and current wind speed and produces a string
indicating the fire risk (low, medium, or high).
Write a program
fire-risk->image that consumes the
fire risk and produces an image of a bear with a colored circle
illustrating the fire risk (red for high, yellow for medium, green for
Start with the following bear image. To use the bear image in your program, right click on the image and save it to your computer. Follow the instructions on the using images page to load the bear image into DrScheme.
The following image shows a possible output from
'low). You should not download this image -- your program should
build something similar to this image.
(Don't worry about getting the background colors just right -- on some machines, the bear background has come up grayish -- you do not need to match that background color.)
If your solution looks complicated, consider whether you could have used additional smaller functions to compute the part of the answer that is different in each case.
Everybody should be able to finish up to this point during lab. Finish all exercises that you don't finish during lab on your own time (we won't always ask you to do this, but the last problem illustrates a point about program design that you need to learn to think about).
Write a program
image->fire-risk that takes an
image illustrating the fire risk and returns a string indicating the
fire risk shown in the image. [HINT: use the image-inside? operator.
Look it up in helpdesk if necessary.]
In the first lecture, we talked about a program that searches for political bias in journalism. Part of that program involved seeing whether the labels "liberal" or "conservative" appeared near names of politicians known to be in each category.
Write a function
label-near? that takes a political
label, a name and three words (all as strings) and produces a boolean
indicating whether both the name and the label appear (in any order)
within the three words.
Write a function
string-one-of? that takes a string of
a word to look for and three strings of other words and produces a
boolean indicating whether the first word is one of the three other
given words. Then write
label-near2? that has the same
contract and purpose as
label-near?, but is implemented
preferable and why? What if we decided to expand the search to four
adjacent words instead of three -- which program would you rather
modify and why? What does this teach you about program design?