Section 13

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 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 exp-1 . .. exp-n) stands for a series of n cons expressions: (cons exp-1 (cons . .. (cons exp-n 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/curriculum-Z-H-17.html (1 of 6) [2/5/2008 4:46: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 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
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.

Page1 / 6

Section 13 - 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