A method of designing functions which
The HtDF recipe consists of the following steps:
Signature → declares the data types (e.g. int
,string
etc.) which the function will accept as input and produce as output. Do try to make this as specific as possible.
;; Number -> Number
; A signature which is more specific would be something like:
;; Integer -> Float
Purpose → a 1-line description to what the function produces as output in terms of its input.
Boolean
, the purpose needs to be very clear about what a true
result would mean (which would, in turn, imply that a false
result has the converse meaning). An example of this can be seen in the 2nd example below.;; Produces a number which is 2 times of the input number.
;; Produces true if the image is tall
Stub → a temporary scaffold for the actual function; it is a function definition with the
(define (double n) 0)
<aside> 💡 Take note that it’s very important to run the stub.
Although the stub is clearly an incomplete function, running it helps you ensure that you have the correct function name, number of parameters, and output type. Ensuring that these things are correct is a simple task early on. If they’re bugged and you don’t find out till later, debugging them would become much more difficult.
</aside>
<aside> 💡 Every step of the recipe will support subsequent steps.
In the examples above, the signature helps us write both the purpose and the stub. Number -> Number
tells us that the function double
takes in 1 number and produces another number — a detail which is important when describing the function’s purpose.
It also tells us that double
only takes in 1 parameter (a Number
). This aspect of the signature indicates that we should be only include 1 parameter in the function stub.
Lastly, the signature is also important for examples contained in check-expect
structures — it helps to ensure that the I/O data types are accurately reflected in the example.
</aside>