When 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 empty list and the symbol 'Mercury. Figure
(cons 'Mercury empty)
'Mercury empty (cons 'Venus (cons 'Mercury empty))
'Venus
'Mercury empty (cons 'Earth (cons 'Venus (cons 'Mercury empty)))
'Earth
'Venus
'Mercury empty
Once we have a list with one item on it, we can construct lists with two items by using cons again:
(cons 'Venus (cons 'Mercury empty))The middle row of figure
Finally, we construct a list with three items:
(cons 'Earth (cons 'Venus (cons 'Mercury empty)))The last row of figure
Create Scheme lists that represent
We can also make lists of numbers. As before, empty is the list without any items. Here is a list with 10 numbers:
(cons 0
(cons 1
(cons 2
(cons 3
(cons 4
(cons 5
(cons 6
(cons 7
(cons 8
(cons 9 empty))))))))))
To build it requires 10 list constructions and one empty list.
In general a list does not have to contain values of one kind, but may contain arbitrary values:
(cons 'RobbyRound
(cons 3
(cons true
empty)))
Here the first item is a symbol, the second one is a number, and the last
one a boolean. We could think of this list as the representation of a
personnel record that includes the name of the employee, the number of
years spent at the company, and whether the employee has health insurance
through the company plan.
Now suppose we are given a list of numbers. One thing we might wish to do is add up the numbers on the list. To make this more concrete, let us assume that we are only interested in lists of three numbers:
A list-of-3-numbers is
(cons x (cons y (cons z empty))) where x, y, and z are numbers.
We write down the contract, purpose, header, and examples as before:
;; add-up-3 : list-of-3-numbers -> number ;; to add up the three numbers in a-list-of-3-numbers ;; examples and tests: ;; (= (add-up-3 (cons 2 (cons 1 (cons 3 empty)))) 6) ;; (= (add-up-3 (cons 0 (cons 1 (cons 0 empty)))) 1) (define (add-up-3 a-list-of-3-numbers) ...)To define the body, however, presents a problem. A constructed list is like a structure. Hence we should layout a template with selector expressions next. Unfortunately, we don't know how to select items from a list.
In analogy to structure selectors, Scheme implements operations for
extracting the fields from a constructed list: first and
rest.
The
first operation extracts the item that we used to
construct a list; the rest operation extracts the list
field.
To describe how first, rest, and cons are related, we can use equations that are similar to the equations that govern addition and subtraction and structure creation and field extraction:
The last one demonstrates how to evaluate nested expressions. The key is to think of (cons a-value a-list) as a value. And, as always, we start with the evaluation of the innermost parenthesized expressions that can be reduced, just as in arithmetic. In the above calculations, the expressions that are about to be reduced next are underlined.= 10
= empty
(first
) =
= 22
Using first and rest we can now write down a template for add-up-3:
;; add-up-3 : list-of-3-numbers -> number ;; to add up the three numbers in a-list-of-3-numbers (define (add-up-3 a-list-of-3-numbers) ... (first a-list-of-3-numbers) ... ... (first (rest a-list-of-3-numbers)) ... ... (first (rest (rest a-list-of-3-numbers))) ... )The three expressions remind us that the input, called a-list-of-3-numbers, contains three components and how to extract them.
Exercise 9.1.2
(cons 10 (cons 20 (cons 5 empty)))What are the values of the following expressions?
Exercise 9.1.3
Finish the development of add-up-3, that is, define the body and test the complete function on some examples.
A list of three numbers is one possible representation for 3-dimensional points. The distance of a 3-dimensional point to the origin of the coordinate grid is computed in the same manner as that of 2-dimensional point: by squaring the numbers, adding them up, and taking the square root.
Use the template for add-up-3 to develop distance-to-0-for-3, which computes the distance of a 3-dimensional point to the origin. Solution
Exercise 9.1.4
Provide a data definition for lists of two symbols. Then develop the
function contains-2-doll?, which consumes a list of two symbols and
determines whether one of them is 'doll. Solution
On the Precise Relationship between Cons and Structures: The discussion of cons, first, and rest suggests that cons creates a structure and first and rest are ordinary selectors:
(define-struct pair (left right))(define (our-cons a-value a-list) (make-pair a-value a-list))
(define (our-first a-pair) (pair-left a-pair))
(define (our-rest a-pair) (pair-right a-pair))
(define (our-cons? x) (pair? x))
Although these definitions are a good first approximation, they are inaccurate in one important point. DrScheme's version of cons is really a checked version of make-pair. Specifically, the cons operation ensures that the right field is always a list, that is, constructed or empty. This suggests the following refinement:
(define (our-cons a-value a-list)
(cond
[(empty? a-list) (make-pair any a-list)]
[(our-cons? a-list) (make-pair any a-list)]
[else (error 'cons ``list as second argument expected'')]))
The definitions for our-first, our-rest, and
our-cons? remain the same. Finally, we must also promise not to
use make-pair directly so that we don't accidentally build a bad
list.