This preview shows pages 1–3. Sign up to view the full content.
How to Design Programs: An Introduction to Computing and Programming [Go to first, previous, next page; contents; index]Section 9Compound Data, Part 2: ListsStructures are one way to represent compound information. They are useful when we know how many pieces of data we wish to combine. In many cases, however, we don't know how many things we wish to enumerate, and in that case we form a list. A list can be of arbitrary length, that is, it contains a finite, but undetermined number of pieces of data. Forming lists is something that all of us do. Before we go grocery shopping, we often write down a list of items that we want to purchase. When we plan out a day in the morning, we write down a list of things to do. During December, many children prepare Christmas wish lists. To plan a party, we list the people we want to invite. In short, arranging information in the form of lists is a ubiquitous part of our life, and we should learn to represent lists as Scheme data. In this section, we first learn to create lists and then move on to developing functions that consume lists.9.1 ListsWhen we form a list, we always start out with the empty list. In Scheme, emptyrepresents the empty list. From here, we can construct a longer list with the operation cons. Here is a simple example: (cons 'Mercury empty) In this example, we constructed a list from the emptylist and the symbol 'Mercury. Figure 25 presents this list in the same pictorial manner we used for structures. The box for conshas two fields: firstand rest. In this specific example the firstfield contains 'Mercuryand the restfield contains empty. (1 of 19) [2/5/2008 4:45:35 PM]
has intentionally blurred sections.
Sign up to view the full version.