Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Getting Started –

...

DrScheme, Design Recipe

Original Author: Dr. John Greiner

...

  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
    ;; Returns purpose:
    ;; (owe n) returns how much money you owe for eating the given number of
    ;;n slices of pizza.
    ;; definition:
    (define (owe slices)
       (* (/ 12 8) slices))
    
    Observe that DrScheme 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. DrScheme 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 DrScheme 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 DrScheme 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 DrScheme 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\Lab01 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?

...

  1. Write the function's contract, purpose, and header. The contract specifies the function name and what kind of values the function will use as input and output. The purpose is a short statement describing what (not how) the function will calculate. The header is the part of the function definition specifying the function and parameter names. Type these in the definition window. Put the contract and purpose in comments, as in the following examples:
    Code Block
          ;; contract:
          ;; owe : nat -> num
          ;;
          ;; purpose:
          ;; (owe n) returns how much money you owe for eating n pizza slices.
          ;;
          ;; definitionheader:
          ;; (define (owe slices) ...)
      
    We won't use DrScheme to verify that our contracts are correct, although that is a very useful thing to do. Look for that in COMP 212.
  2. Make some examples of what the function should docompute. You need to make sure you understand what the function is supposed to do before you define it. When applicable, use some of the example data. We recommend the following form for these examples:
    Code Block
          ;; Examples:
          ;; (owe 0) => 0
          ;; (owe 10) => 15
       
    The first line provides an explanation of the following, making it more readable. (We haven't introduced symbols yet officially -- we will see them in class soon.) The next two lines each give an example, asking DrScheme to compare (with the numeric equality function =) the actual result with the expected result. You should pick enough examples to test the function adequately. We'll say more about that later, but this should include any interesting inputs like zero.Write the function body. Soon, we will have lots to say about this step. For now, for programs on "unstructured" data, this is very straightforward, because we are typically given an equation like … code
    
          (define (owe slices)
             (* (/ 12 8) slices))
       
    For sets of intervals
  3. or other conditional functions, there should be exactly one condition clause per option.Test the function. i.e., make sure the previous examples really work. If you wrote the examples as suggested above, you can use them to test the function. Add a test section after your function definitionexamples, like this:
    Code Block
          "Testing;; oweTests:"
          (equal?check-expect (owe 0) 0))
          (equal?check-expect (owe 10) 15))
       
    All of these tests should return true pass , assuming that the tests themselves are correct. If your program doesn't work, either fix it now or put that in comments too, to remind yourself to fix it later, and to let your grader/boss/customer/whomever know also. The better code for owe is correct. The more elaborate your test, the more errors you'll catch sooner and the less time it will take to write a correct function. Note that at this point owe has not been written yet. Still we write the test code first! This is one of the key steps in modern software development: test before coding. It is called test-driven development. This is where we deviate from the design recipe describe in HTDP (the textbook). We write the test code first!
  4. Write the function body. Soon, we will have lots to say about this step. For now, for programs on "unstructured" data, this is very straightforward, because we are typically given an equation like …
    Code Block
    
          ;; definition:
          (define (owe slices)
             (* (/ 12 8) slices))
       
    For sets of intervals or other conditional functions, there should be exactly one condition clause per option.

Using the design recipe, define a function calculating the area of a right isosceles triangle.

...

  1. Try calling some the built-in operations like <=, =, and, or, not, equal? on various data values, e.g., is 17 times 18 bigger than 256?
  2. What are the contracts of these built-in functions?
    *Choose any two, and write down the contract.
    *Verify your guess by asking the labby
    or by using DrScheme's Help Desk to look at the
    manual for the Beginning Student language.
    *Practice writing a function which returns a Boolean:
    Following the design recipe,
    write
    Code Block
    within-two?
    which takes in two numbers
    m and n, and returns true if
    m - n and {{ n - m}} are both
    less than 2.
    Otherwise, it should return false.
    (For learning purposes only, don't use abs,
    which computes the absolute value. Also, don't use
    cond or if.)
    Your examples/tests can look like
    Code Block
    
         (boolean=?check-expect (within-two? 99.8 101) true)
         (boolean=?check-expect (within-two? 5 -5)     false)
                

...

You'll also need to know how to use SVN. On the SVN Turnin Page we have basic instructions for using SVN to turn in your homeworks.

Always "log out" from your account when you are done and leaving. Otherwise, someone can use your account, e.g., to copy or delete your assignments. Be sure to save everything before you log out!!

...

...