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 13
Intermezzo 2: List Abbreviations
[../icons/plt.gif]
Intermediate
Student
Using
cons
to create lists is cumbersome if a list contains many items. Fortunately, Scheme provides the
list
operation, which consumes an arbitrary number of values and creates a list. Here is Scheme's extended
syntax:
<prm> = list
The extended collection of values is
<val> = (list <val> .
.. <val>)
A simpler way to understand
list
expressions is to think of them as abbreviations. Specifically, every
expression of the shape
(list exp1 .
.. expn)
stands for a series of
n
cons
expressions:
(cons exp1 (cons .
.. (cons expn empty)))
Recall that
empty
is not an item of the list here, but the rest of the list. Here are three examples:
(list 1 2)
= (cons 1 (cons 2 empty))
(list 'Houston 'Dallas 'SanAntonio)
= (cons 'Houston (cons 'Dallas (cons 'SanAntonio empty)))
(list false true false false)
= (cons false (cons true (cons false (cons false empty))))
file:///C/Documents%20and%20Settings/Linda%20Grauer.
../How%20to%20Design%20Programs/curriculumZH17.html (1 of 6) [2/5/2008 4:46:35 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentHow to Design Programs: An Introduction to Computing and Programming
They introduce lists with two, three, and four items, respectively.
Of course, we can apply
list
not only to values but also to expressions:
(list (+ 0 1) (+ 1 1))
= (list 1 2)
Before the list is constructed, the expressions must be evaluated. If during the evaluation of an expression an
error occurs, the list is never formed:
(list (/ 1 0) (+ 1 1))
= /: divide by zero
In short,
list
behaves just like any other primitive operation.
The use of
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '07
 Fisler

Click to edit the document details