Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Type (or copy/paste) the following expressions into the interactions window
    Code Block
    (+ 2 7)
    
    Code Block
    (* (+ 2 7) 3)
    
  2. Type (or copy/paste) the following blocks of code into the definition window, including:
    Code Block
    ;; contract:
    ;; owe: nat -> num
    ;; purpose:
    ;; (owe n) returns how much money you owe for eating n slices of pizza.
    ;; definition:
    (define (owe slices)
       (* (/ 12 8) slices))
    
    Observe that DrRacket helps you with the indentation if you use the Return key in appropriate places. It also "bounces" the cursor to visually match the closing parenthesis with the corresponding opening parenthesis.
  3. Use the definition of owe by typing in the interaction window some Scheme expressions using those definitions, e.g.,
    Code Block
    (owe 5)
    
    (owe 7)
    
  4. We included comments before each function definition, explaining what it is supposed to do.  DrRacket ignores the comments, but you, your grader, your boss, etc., can read them. Soon, we'll provide more guidance on what to say in your comments.
    Code Block
    ; One form of comments is anything following a semicolon
    ; up to the end of the line.
    ;; However, you'll often see two semicolons starting a comment.
    
    #|
      Another form of comments is anything between these
      two matching markers.
    |#
    
    You can also go to the Special menu, choose Insert comment box. You are welcome to do this, but one caveat: if
    DrRacket
    saves a file which includes a fancy comment box, the file will not be saved in plain-text format. (This is only an issue if you want to open or print the file with a different program later.)
  5. Edit your definitions or comments some. You can use the mouse, the arrow keys, backspace, and standard keyboard shortcuts to move the cursor around. Also, the PC, Mac, and Unix versions of DrRacket each use their respective standard keyboard shortcuts. (You can look in the Edit menu under Keybindings for the complete list.)
  6. Load the definitions into the interaction window by clicking on the Run button. If DrRacketdetects DrRacket detects any syntactic errors, it will give you an error message in the interactions window and highlight the error in the definitions window.
  7. Save your work in your Comp211\Labs\Lab00 directory. Be sure to use a filename that has a .ss extension. You don't need to turn in your lab work. In fact, you don't have to save it, but we recommend it so that you can look at it again.
  8. Finally, intentionally introduce an error or, what you think will be an error, press Run, and see what error message is given. We'll go around the room to see what different error messages people get. Do the error messages tend to make sense?

DrRackethas DrRacket has lots of other features, including a complete manual. We'll explore more of DrRacketin DrRacket in later labs, and we encourage you to explore, but there's no need to learn all its features. Read the DrScheme Tips and Traps page for helpful information on common mistakes, error messages, etc.

...

Step-by-step Evaluation

While DrRacketevaluates DrRacket evaluates our Scheme programs for us, it is also important for us to understand the process of evaluation. The details of evaluation will be covered in class. Here in lab, we want to explore two useful tools to help us:

  • !DrRacket's stepping tool that illustrates evaluation.
  • A technique for the programmer to illustrate and check evaluation "by hand".

...

  1. Place an example use or two of your functions in the definitions window, after the appropriate definitions. For example,
    Code Block
          (owe 10)
       
  2. Click on the Step button. This brings up a new stepper windows which will show how DrRacketcalculates how DrRacket calculates the answer.
  3. Use the stepper's button to look through the evaluation. At each step, it shows what part of the code needs to be evaluated next, and what the result is.

...

"Hand-evaluation" is just you doing the same thing as DrRacket's stepper. It is useful to convince yourself that you know what is supposed to happen, independently of having DrRackethelp DrRacket help you.

  1. Hand-evaluate a few example expressions, and type in each of the small steps you would make to calculate the result. Type these in the definitions window after your definitions. For example,
    Code Block
    ;; Hand-evaluation:
    
    (owe 10)
    
    (* (/ 12 8) 10)
    
    (* 3/2 10)
    
    15
     
    This is just high school algebra, successively simplfying expressions, until you reach a final, simple value.
    • What do you hope to see, when you Run all these expressions? What would you see if there were a mistake?
    • While you are doing this, it is often helpful to copy the previous step, and edit this copy for the current step. This can save you some typing, and it helps eliminate mistakes. You'll want the Copy and Paste features in the Edit menu.
  2. Verify your steps by clicking Run.
  3. Once you've used this to verify your steps, put the steps in comments, rather than deleting them. (Later, you might change the code, and you want to re-use these as test cases.)

...