...
- 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:
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.Code Block ;; contract: ;; owe : nat -> num ;; ;; purpose: ;; Returns (owe n) returns how much money you owe for eating n thepizza givenslices. ;; number of pizza slices. ;; definition: (define (owe slices) ...)
- Make some examples of what the function should do. 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:
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.Code Block ;; Examples: ;; (owe 0) => 0 ;; (owe 10) => 15
- 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 …
For sets of intervals or other conditional functions, there should be exactly one condition clause per option.Code Block (define (owe slices) (* (/ 12 8) slices))
- 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 definition, like this:
All of these tests should returnCode Block "Testing owe:" (equal? (owe 0) 0)) (equal? (owe 10) 15))
true
, 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 your test, the more errors you'll catch sooner and the less time it will take to write a correct function.
...