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 [(symbol=? lang 'spanish) 'hola] [(symbol=? lang 'french) 'bonjour] [(symbol=? lang 'pig-latin) 'ellohay]))
Run the program by typing (babel 'french) at the prompt in the interactions window.
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 recall the last expression 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: 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).
Using DrScheme's HelpDesk (not WPI's helpdesk!): Under the help menu, select the "HelpDesk" option. In the "Search for" box, enter define-struct and set the options to "keyword or index entry". You'll get a series of links to documentation on this keyword, broken down by language levels. Click on the link under the beginner language. You can also try other searches on operator names (such as remainder or symbol=? to get familiar with helpdesk).
If you want to see a list of all the constructs and operators that are defined in beginner language, follow the "Beginning Student Language" link at the bottom of the window on define-struct.
If you get a long, seemingly garbled collection of info back from a search, make sure you are using some option that does not include a text search. The other searches give much more concise output and will usually be sufficient.
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!). 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.
In order to get more familiar with structures and data models, we are going to define data models and programs for geometric shapes on a coordinate plane. We want to start with a set of shapes containing circles and rectangles. A circle needs a center point and a radius; a rectangle needs the point at the top-left corner, a width, and a height.
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! You should use posns to capture coordinates in the following exercises.
Develop data models for circles, rectangles, and shapes. Write down any define-structs necessary to support your definitions. Write down the names and contracts of the functions that DrScheme defines for you based on your define-structs.
Create examples of data for each define-struct you created for the previous problem.
Write a program area which consumes a shape and produces a number (the area of the shape). Test your program on both circles and rectangles.
Everybody should be able to finish up to this point
Use the stepper to trace the evaluation of your area program on a shape.
Write a program move-shape which consumes a shape and two numbers and produces a shape. The first number is the amount to move the shape along the x-axis and the second number is the amount to move the shape along the y-axis.
Drawing packages usually allow you to glue several shapes together so they can be manipulated as one shape. Add a compound-shape type to your list of shapes, where a compound-shape glues two existing shapes together. Add the appropriate data models and modify your existing data models accordingly. Then modify your move-shape function to also handle compound-shapes.
If you are doing very well with data models and programming with structs, you should be able to finish up to this point
Write a program compound-duplicate that takes a shape and a number and produces a compound shape. The compound shape should contain two copies of the given shape, with the second distanced from the original along the horizontal axis by the given number (and at the same place on the vertical axis).