How to Design Programs: An Introduction to Computing and Programming
Designing Abstractions from Examples
When we first learn to add, we use concrete examples. Later on, we study how to add two arbitrary numbers; that is, we
form an abstraction of the addition operation. Much later still, we learn to formulate abstractions directly as
expressions: expressions that compute the wage of some employee, expressions that convert temperatures, or
expressions that determine the area of a geometric shape. In short, we initially go from concrete examples to
abstraction, but eventually we learn to form abstractions directly without thinking (much) about concrete instances.
In this section, we discuss a design recipe for creating abstractions from concrete examples. Later, in sections
we study additional approaches to function abstraction.
Abstracting from Examples
Forming abstractions from examples is easy. As we have seen in section
, we start from two concrete function
definitions, compare them, mark the differences, and abstract. Let us formulate these steps as a recipe:
When we find two function definitions that are almost the same except at a few places and for their names, we
compare them and mark the differences with boxes. If the boxes contain only values, we can abstract.
Warning: Abstracting over Non-values
: The recipe requires a substantial modification for non-values.
Here is a pair of similar function definitions:
;; convertCF : lon
(define (convertCF alon)
[(empty? alon) empty]
C->F (first alon))
(convertCF (rest alon)))]))
;; names : loIR
(define (names aloIR)
[(empty? aloIR) empty]
IR-name (first aloIR))
(names (rest aloIR)))]))
The two functions apply a function to each item in a list. They differ in only one aspect: what they apply to each item
on the list. The two boxes emphasize the difference. Each contains a functional value, so we can abstract.
Next we replace the contents of corresponding pairs of boxes with new names and add these names to the
parameter list. For example, if there are three pairs of boxes, we need three new names. The two definitions must
now be the same, except for the function name. To obtain the abstraction, we systematically replace the function
..How%20to%20Design%20Programs/curriculum-Z-H-27.html (1 of 12) [2/5/2008 4:50:32 PM]