This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 14 More Self-referential Data Definitions Lists and natural numbers are two classes of data whose description requires self-referential data definitions. Both data definitions consist of two clauses; both have a single self-reference. Many interesting classes of data, however, require more complex definitions than that. Indeed, there is no end to the variations. It is therefore necessary to learn how to formulate data definitions on our own, starting with informal descriptions of information. Once we have those, we can just follow a slightly modified design recipe for self-referential data definitions. 14.1 Structures in Structures Medical researchers rely on family trees to do research on hereditary diseases. They may, for example, search a family tree for a certain eye color. Computers can help with these tasks, so it is natural to design representations of family trees and functions for processing them. One way to maintain a family tree of a family is to add a node to the tree every time a child is born. From the node, we can draw connections to the node for the father and the one for the mother, which tells us how the people in the tree are related. For those people in the tree whose parents are unknown, we do not draw any connections. The result is a so-called ancestor family tree because, given any node in the tree, we can find the ancestors of that person if we follow the arrows but not the descendants. As we record a family tree, we may also want to record certain pieces of information. The birth date, birth weight, the color of the eyes, and the color of the hair are the pieces of information that we care about. Others record different information. [curriculum2-Z-G-1.gif] Figure 35: A sample ancestor family tree See figure 35 for a drawing of an ancestor family tree. Adam is the child of Bettina and Carl; he has yellow eyes and was born in 1950. Similarly, Gustav is the child of Eva and Fred, has brown eyes, and was born in 1988. To represent a child in a family tree is to combine several pieces of information: information file:///C|/Documents%20and%20Settings/Linda%20Graue...How%20to%20Design%20Programs/curriculum-Z-H-19.html (1 of 20) [2/5/2008 4:47:03 PM] How to Design Programs: An Introduction to Computing and Programming about the father, the mother, the name, the birth date, and eye color. This suggests that we define a new structure: (define-struct child (father mother name date eyes)) The five fields of child structures record the required information, which suggests the following data definition: A child is a structure: (make-child f m na da ec) where f and m are child structures; na and ec are symbols; and da is a number....
View Full Document
- Spring '07
- Web page, World Wide Web, Design Programs