How to Design Programs: An Introduction to Programming and Computing

Info icon 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 9 Compound Data, Part 2: Lists Structures 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 Lists When we form a list, we always start out with the empty list. In Scheme, empty represents 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 cons tructed a list from the empty list and the symbol 'Mercury . Figure 25 presents this list in the same pictorial manner we used for structures. The box for cons has two fields: first and rest . In this specific example the first field contains 'Mercury and the rest field contains empty . (1 of 19) [2/5/2008 4:45:35 PM]
Image of page 1

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