We will follow the coding style and design recipe set forth in How to Design Programs (HtDP). A summary of the salient points regarding the recipe and style follows.
CONTRACT
Programs consume and produce data. Regardless of the language we are programming in, we need to provide a meaningful name to our functions as well as understand the type of that function's inputs and outputs. In HtDP terminology, we start with a contract.
;; CONTRACT
;; area-of-triangle : number number -> number
The contract tells us that we are writing the function area-of-triangle, that it consumes two numbers, and returns a number.
PURPOSE
Next, you should be able to write a purpose statement for any function that you write. If you cannot, then you don't understand what it does. Also, if the purpose statement becomes overly verbose, it is likely that the function does too much.
;; CONTRACT
;; area-of-triangle : number number -> number
;; PURPOSE
;; Calculates the area of a triangle given its base and height.
STUB (HEADER)
Once you have written a contract and purpose statement, you can write a header that will fail on all inputs. This is primarily to allow us to move on to the next step...
;; CONTRACT
;; area-of-triangle : number number -> number
;; PURPOSE
;; Calculates the area of a triangle given its base and height.
(define (area-of-triangle base height)
'...)
TESTS
Before we write any code, we should write test cases. Test cases force us to precisely understand the correspondence between inputs and outputs for every function we write. If you cannot write good test cases, you cannot write good code.
By writing our tests first, we know that they will all fail. If our tests are good, then they will all pass upon completion. This is a test-first methodology for acceptance. Failure tests (tests that are expected to fail), performance tests and the like will have to be the subject of later discussions.
;; CONTRACT
;; area-of-triangle : number number -> number
;; PURPOSE
;; Calculates the area of a triangle given its base and height.
(define (area-of-triangle base height)
'...)
;; TESTS
(test 7.5 (area-of-triangle 3 5))
(test 10 (area-of-triangle 2 10))
(test 0 (area-of-triangle 0 0))
At this point, you should be able to execute your code and see the following in your interactions window:
(bad 7.5 (area-of-triangle 3 5) "at line 9")
(bad 10 (area-of-triangle 2 10) "at line 10")
(bad 0 (area-of-triangle 0 0) "at line 11")
IMPLEMENTATION
Your implementation should follow the HtDP design recipe further. In particular, you should stub in conditions for all data variants (if you are matching on a language grammar, for example), letting your code follow the structure of the data your function is operating on.
;; CONTRACT
;; area-of-triangle : number number -> number
;; PURPOSE
;; Calculates the area of a triangle given its base and height.
(define (area-of-triangle base height)
(/ (* base height) 2))
;; TESTS
(test 7.5 (area-of-triangle 3 5))
(test 10 (area-of-triangle 2 10))
(test 0 (area-of-triangle 0 0))
Once your implementation is complete, you should be able to execute your code, and watch all of your tests pass. By all means, you and your partner should go for a vend at this point.
