Versions Compared

Key

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

...

  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.
          ;;
          ;; header:
          ;; (define (owe slices) ...)
      
    We won't use DrRacketto 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 compute. 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 DrRacketto 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.
  3. 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 examples, like this:
    Code Block
          ;; Tests:
          (check-expect (owe 0) 0)
          (check-expect (owe 10) 15)
       
    All of these tests should pass , assuming that the 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.

...