SICP of the Day 12/14
Today’s post comes after the heading “What Is Meant By Data?” The context is a rational number arithmetic package introduced at the beginning of the chapter.
We began the rational-number implementation in section 2.1.1 by implementing the rational-number operations
add-rat,sub-rat, and so on in terms of three unspecified procedures:make-rat,numer, anddenom. At that point, we could think of the operations as being defined in terms of data objects — numerators, denominators, and rational numbers — whose behavior was specified by the latter three procedures.But exactly what is meant by data? It is not enough to say “whatever is implemented by the given selectors and constructors.” Clearly, not every arbitrary set of three procedures can serve as an appropriate basis for the rational-number implementation. We need to guarantee that, if we construct a rational number
xfrom a pair of integersnandd, then extracting thenumerand thedenomof x and dividing them should yield the same result as dividingnbyd. In other words,make-rat,numer, anddenommust satisfy the condition that, for any integernand any non-zero integerd, ifxis(make-rat n d), then(numer x) n --------- = - (denom x) dIn fact, this is the only condition
make-rat,numer, anddenommust fulfill in order to form a suitable basis for a rational-number representation. In general, we can think of data as defined by some collection of selectors and constructors, together with specified conditions that these procedures must fulfill in order to be a valid representation.