Define, run, and save programs in DrScheme
Use DrScheme's debugging tools: the stepper and check syntax
Write simple programs with structures
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. 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 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 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 green, red, and blue. Built-in operators and atoms appear in blue. User-defined identifiers appear in green. Identifiers that DrScheme doesn't recognize appear in red. Your misspelled identifier should appear in red.
Move your cursor over the green 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.
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 (symbol=? (boa-food aboa) 'mice)(symbol=? (boa-food aboa) 'rats)(symbol=? (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, leave the answer out of a cond clause) 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?
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.
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.