Section 9

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 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 . file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-13.html (1 of 19) [2/5/2008 4:45:35 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 (cons 'Mercury empty) 'Mercury empty (cons 'Venus (cons 'Mercury empty)) 'Venus 'Mercury empty (cons 'Earth (cons 'Venus (cons 'Mercury empty))) 'Earth 'Venus 'Mercury empty Figure 25: Building a list Once we have a list with one item on it, we can cons truct lists with two items by using cons again: (cons 'Venus (cons 'Mercury empty)) The middle row of figure 25 shows how we should imagine the second list. It is also a box of two fields, but this time the rest field contains a box. Indeed, it contains the box from the top row of the same figure. Finally, we construct a list with three items: (cons 'Earth (cons 'Venus (cons 'Mercury empty))) The last row of figure 25 illustrates the list with three items. Its rest field contains a box that contains a box again. So, as we create lists we put boxes into boxes into boxes, etc. While this may appear strange at first glance, it is just like a set of Chinese gift boxes or a set of nested drinking cups, which we sometimes get for our early birthdays. The only difference is that Scheme programs can nest lists much deeper than any artist could nest physical boxes. Exercise 9.1.1.
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 / 19

Section 9 - How to Design Programs An Introduction to Computing and Programming[Go to first previous next page contents index Section 9 Compound

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