Section 15

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 15 Mutually Referential Data Definitions In the preceding section, we developed data representations of family trees, Web pages, and Scheme expressions. Developing functions for these data definitions was based on one and the same design recipe. If we wish to develop more realistic representations of Web pages or Scheme expressions, or if we wish to study descendant family trees rather than ancestor trees, we must learn to describe classes of data that are interrelated. That is, we must formulate several data definitions at once where the data definitions not only refer to themselves, but also refer to other data definitions. 15.1 Lists of Structures, Lists in Structures When we build a family tree retroactively, we often start from the child's perspective and proceed from there to parents, grandparents, etc. As we construct the tree, we write down who is whose child rather than who is whose parents. We build a descendant family tree . Drawing a descendant tree proceeds just like drawing an ancestor tree, except that all arrows are reversed. Figure 40 represents the same family as that of figure 35 , but drawn from the descendant perspective. [curriculum2-Z-G-3.gif] Figure 40: A descendant family tree Representing these new kinds of family trees and their nodes in a computer requires a different class of data than do the ancestor family trees. This time a node must include information about the children instead of the two parents. Here is a structure definition: (define-struct parent (children name date eyes)) The last three fields in a parent structure contain the same basic information as a corresponding child structure, but the contents of the first one poses an interesting question. Since a parent may have an arbitrary number of children, the children field must contain an undetermined number of nodes, each file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-20.html (1 of 12) [2/5/2008 4:47:18 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming of which represents one child. The natural choice is to insist that the children field always stands for a list of parent structures. The list represents the children; if a person doesn't have children, the list is empty . This decision suggests the following data definition: A parent is a structure: (make-parent loc n d e) where loc is a list of children, n and e are symbols, and d is a number. Unfortunately, this data definition violates our criteria concerning definitions. In particular, it mentions the name of a collection that is not yet defined: list of children. Since it is impossible to define the class of parents without knowing what a list of children is, let's start
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Spring '07 term at WPI.

Page1 / 12

Section 15 - How to Design Programs: An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online