Course notes for “How to Code: Simple Data” on edX.
What makes it difficult to write programs?
Systematic program design is the path from poorly-formed problem to well-structured solution.
The HtDF recipe systematizes the design of a function. (Note: The recipe is going to make hard functions easier to design and easy functions a little harder to design.)
To design a single function:
check-expect
The signature declares the type of data a function consumes and produces. Always use the most specific type.
The purpose is a one line description of what the function produces in terms of what it consumes.
The stub is a function definition that has the correct function name, has the correct number of parameters, and produces a dummy result of the correct type.
Examples help us understand what the function must do. Multiple examples illustrate behavior. Wrapping in check-expect
means they will also serve as unit tests for the completed function.
The template is the outline of the function.
Use the previous recipe steps to code the function body.
When we design data, we make decisions about all of the functions that operate on that data.
Data definitions explain how information is represented as data. We represent information in the problem domain using data in the program.
The first step of the recipe is to identify the inherent structure of the information.
Once that is done, a data definition consists of four or five elements: