...
- 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: ;; (owe n) returns how much money you owe for eating n pizza slices. ;; ;; definitionheader: ;; (define (owe slices) ...)
- 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:
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
- 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:
All of these tests should pass , assuming that the code forCode Block ;; Tests: (check-expect (owe 0) 0) (check-expect (owe 10) 15)
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 pointowe
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! - 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 ;; definition: (define (owe slices) (* (/ 12 8) slices))
...