![]() |
James Thornton |
| Internet Business Consultant |
| Home | Blog | Bio | Projects | Contact | Latest Blog (new site): How to Get to Genius |
|---|
5. ListsA list represents a sequence of zero or more elements (which may be any Lisp objects). The important difference between lists and vectors is that two or more lists can share part of their structure; in addition, you can insert or delete elements in a list without copying the whole list.
5.1 Lists and Cons CellsLists in Lisp are not a primitive data type; they are built up from cons cells. A cons cell is a data object that represents an ordered pair. That is, it has two slots, and each slot holds, or refers to, some Lisp object. One slot is known as the CAR, and the other is known as the CDR. (These names are traditional; see 2.3.6 Cons Cell and List Types.) CDR is pronounced "could-er." We say that "the CAR of this cons cell is" whatever object its CAR slot currently holds, and likewise for the CDR.
A list is a series of cons cells "chained together," so that each
cell refers to the next one. There is one cons cell for each element of
the list. By convention, the CARs of the cons cells hold the
elements of the list, and the CDRs are used to chain the list: the
CDR slot of each cons cell refers to the following cons cell. The
CDR of the last cons cell is Because most cons cells are used as part of lists, the phrase list structure has come to mean any structure made out of cons cells.
The symbol The CDR of any nonempty list l is a list containing all the elements of l except the first.
5.2 Lists as Linked Pairs of Boxes
A cons cell can be illustrated as a pair of boxes. The first box
represents the CAR and the second box represents the CDR.
Here is an illustration of the two-element list,
Each pair of boxes represents a cons cell. Each box "refers to",
"points to" or "holds" a Lisp object. (These terms are
synonymous.) The first box, which describes the CAR of the first
cons cell, contains the symbol The same list can be illustrated in a different sort of box notation like this:
Here is a more complex illustration, showing the three-element list,
The same list represented in the first box notation looks like this:
See section 2.3.6 Cons Cell and List Types, for the read and print syntax of cons cells and lists, and for more "box and arrow" illustrations of lists.
5.3 Predicates on Lists
The following predicates test whether a Lisp object is an atom, is a
cons cell or is a list, or whether it is the distinguished object
5.4 Accessing Elements of Lists
The most common way to compute the length of a list, when you are not
worried that it may be circular, is with
5.5 Building Cons Cells and Lists
Many functions build lists, as lists reside at the very heart of Lisp.
Here is an example of using
You can see how
An empty sequence contributes nothing to the value returned by
This once was the usual way to copy a list, before the function
Here we show the use of vectors and strings as arguments to
With the help of
If no sequences are given,
Here are some examples where the final argument is not a list:
The second example shows that when the final argument is a sequence but not a list, the sequence's elements do not become elements of the resulting list. Instead, the sequence becomes the final CDR, like any other non-list final argument.
5.6 Modifying Existing List Structure
You can modify the CAR and CDR contents of a cons cell with the
primitives Common Lisp note: Common Lisp uses functions
5.6.1 Altering List Elements with
|
(setq x '(1 2))
=> (1 2)
(setcar x 4)
=> 4
x
=> (4 2)
|
When a cons cell is part of the shared structure of several lists, storing a new CAR into the cons changes one element of each of these lists. Here is an example:
;; Create two lists that are partly shared.
(setq x1 '(a b c))
=> (a b c)
(setq x2 (cons 'z (cdr x1)))
=> (z b c)
;; Replace the CAR of a shared link.
(setcar (cdr x1) 'foo)
=> foo
x1 ; Both lists are changed.
=> (a foo c)
x2
=> (z foo c)
;; Replace the CAR of a link that is not shared.
(setcar x1 'baz)
=> baz
x1 ; Only one list is changed.
=> (baz foo c)
x2
=> (z foo c)
|
Here is a graphical depiction of the shared structure of the two lists
in the variables x1 and x2, showing why replacing b
changes them both:
--- --- --- --- --- ---
x1---> | | |----> | | |--> | | |--> nil
--- --- --- --- --- ---
| --> | |
| | | |
--> a | --> b --> c
|
--- --- |
x2--> | | |--
--- ---
|
|
--> z
|
Here is an alternative form of box diagram, showing the same relationship:
x1:
-------------- -------------- --------------
| car | cdr | | car | cdr | | car | cdr |
| a | o------->| b | o------->| c | nil |
| | | -->| | | | | |
-------------- | -------------- --------------
|
x2: |
-------------- |
| car | cdr | |
| z | o----
| | |
--------------
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The lowest-level primitive for modifying a CDR is setcdr:
Here is an example of replacing the CDR of a list with a different list. All but the first element of the list are removed in favor of a different sequence of elements. The first element is unchanged, because it resides in the CAR of the list, and is not reached via the CDR.
(setq x '(1 2 3))
=> (1 2 3)
(setcdr x '(4))
=> (4)
x
=> (1 4)
|
You can delete elements from the middle of a list by altering the
CDRs of the cons cells in the list. For example, here we delete
the second element, b, from the list (a b c), by changing
the CDR of the first cons cell:
(setq x1 '(a b c))
=> (a b c)
(setcdr x1 (cdr (cdr x1)))
=> (c)
x1
=> (a c)
|
Here is the result in box notation:
--------------------
| |
-------------- | -------------- | --------------
| car | cdr | | | car | cdr | -->| car | cdr |
| a | o----- | b | o-------->| c | nil |
| | | | | | | | |
-------------- -------------- --------------
|
The second cons cell, which previously held the element b, still
exists and its CAR is still b, but it no longer forms part
of this list.
It is equally easy to insert a new element by changing CDRs:
(setq x1 '(a b c))
=> (a b c)
(setcdr x1 (cons 'd (cdr x1)))
=> (d b c)
x1
=> (a d b c)
|
Here is this result in box notation:
-------------- ------------- -------------
| car | cdr | | car | cdr | | car | cdr |
| a | o | -->| b | o------->| c | nil |
| | | | | | | | | | |
--------- | -- | ------------- -------------
| |
----- --------
| |
| --------------- |
| | car | cdr | |
-->| d | o------
| | |
---------------
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here are some functions that rearrange lists "destructively" by modifying the CDRs of their component cons cells. We call these functions "destructive" because they chew up the original lists passed to them as arguments, relinking their cons cells to form a new list that is the returned value.
See delq, in 5.7 Using Lists as Sets, for another function
that modifies cons cells.
append (see section 5.5 Building Cons Cells and Lists), the lists are
not copied. Instead, the last CDR of each of the
lists is changed to refer to the following list. The last of the
lists is not altered. For example:
(setq x '(1 2 3))
=> (1 2 3)
(nconc x '(4 5))
=> (1 2 3 4 5)
x
=> (1 2 3 4 5)
|
Since the last argument of nconc is not itself modified, it is
reasonable to use a constant list, such as '(4 5), as in the
above example. For the same reason, the last argument need not be a
list:
(setq x '(1 2 3))
=> (1 2 3)
(nconc x 'z)
=> (1 2 3 . z)
x
=> (1 2 3 . z)
|
However, the other arguments (all but the last) must be lists.
A common pitfall is to use a quoted constant list as a non-last
argument to nconc. If you do this, your program will change
each time you run it! Here is what happens:
(defun add-foo (x) ; We want this function to add
(nconc '(foo) x)) ; |
reverse, nreverse alters its argument by reversing
the CDRs in the cons cells forming the list. The cons cell that
used to be the last one in list becomes the first cons cell of the
value.
For example:
(setq x '(a b c))
=> (a b c)
x
=> (a b c)
(nreverse x)
=> (c b a)
;; The cons cell that was first is now last.
x
=> (a)
|
To avoid confusion, we usually store the result of nreverse
back in the same variable which held the original list:
(setq x (nreverse x)) |
Here is the nreverse of our favorite example, (a b c),
presented graphically:
Original list head: Reversed list:
------------- ------------- ------------
| car | cdr | | car | cdr | | car | cdr |
| a | nil |<-- | b | o |<-- | c | o |
| | | | | | | | | | | | |
------------- | --------- | - | -------- | -
| | | |
------------- ------------
|
The argument predicate must be a function that accepts two
arguments. It is called with two elements of list. To get an
increasing order sort, the predicate should return t if the
first element is "less than" the second, or nil if not.
The comparison function predicate must give reliable results for
any given pair of arguments, at least within a single call to
sort. It must be antisymmetric; that is, if a is
less than b, b must not be less than a. It must be
transitive---that is, if a is less than b, and b
is less than c, then a must be less than c. If you
use a comparison function which does not meet these requirements, the
result of sort is unpredictable.
The destructive aspect of sort is that it rearranges the cons
cells forming list by changing CDRs. A nondestructive sort
function would create new cons cells to store the elements in their
sorted order. If you wish to make a sorted copy without destroying the
original, copy it first with copy-sequence and then sort.
Sorting does not change the CARs of the cons cells in list;
the cons cell that originally contained the element a in
list still has a in its CAR after sorting, but it now
appears in a different position in the list due to the change of
CDRs. For example:
(setq nums '(1 3 2 6 5 4 0))
=> (1 3 2 6 5 4 0)
(sort nums '<)
=> (0 1 2 3 4 5 6)
nums
=> (1 2 3 4 5 6)
|
Warning: Note that the list in nums no longer contains
0; this is the same cons cell that it was before, but it is no longer
the first one in the list. Don't assume a variable that formerly held
the argument now holds the entire sorted list! Instead, save the result
of sort and use that. Most often we store the result back into
the variable that held the original list:
(setq nums (sort nums '<)) |
See section 32.15 Sorting Text, for more functions that perform sorting.
See documentation in 24.2 Access to Documentation Strings, for a
useful example of sort.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A list can represent an unordered mathematical set--simply consider a
value an element of a set if it appears in the list, and ignore the
order of the list. To form the union of two sets, use append (as
long as you don't mind having duplicate elements). Other useful
functions for sets include memq and delq, and their
equal versions, member and delete.
Common Lisp note: Common Lisp has functionsunion(which avoids duplicate elements) andintersectionfor set operations, but GNU Emacs Lisp does not have them. You can write them in Lisp if you wish.
memq returns a list starting with the
first occurrence of object. Otherwise, it returns nil.
The letter `q' in memq says that it uses eq to
compare object against the elements of the list. For example:
(memq 'b '(a b c b a))
=> (b c b a)
(memq '(2) '((1) (2))) ; |
member, except that it ignores
differences in letter-case and text representation: upper-case and
lower-case letters are treated as equal, and unibyte strings are
converted to multibyte prior to comparison.
eq to
object from list. The letter `q' in delq says
that it uses eq to compare object against the elements of
the list, like memq and remq.
When delq deletes elements from the front of the list, it does so
simply by advancing down the list and returning a sublist that starts
after those elements:
(delq 'a '(a b c)) == (cdr '(a b c)) |
When an element to be deleted appears in the middle of the list, removing it involves changing the CDRs (see section 5.6.2 Altering the CDR of a List).
(setq sample-list '(a b c (4)))
=> (a b c (4))
(delq 'a sample-list)
=> (b c (4))
sample-list
=> (a b c (4))
(delq 'c sample-list)
=> (a b (4))
sample-list
=> (a b (4))
|
Note that (delq 'c sample-list) modifies sample-list to
splice out the third element, but (delq 'a sample-list) does not
splice anything--it just returns a shorter list. Don't assume that a
variable which formerly held the argument list now has fewer
elements, or that it still holds the original list! Instead, save the
result of delq and use that. Most often we store the result back
into the variable that held the original list:
(setq flowers (delq 'rose flowers)) |
In the following example, the (4) that delq attempts to match
and the (4) in the sample-list are not eq:
(delq '(4) sample-list)
=> (a c (4))
|
The following two functions are like memq and delq but use
equal rather than eq to compare elements. See section 2.7 Equality Predicates.
member tests to see whether object is a member
of list, comparing members with object using equal.
If object is a member, member returns a list starting with
its first occurrence in list. Otherwise, it returns nil.
Compare this with memq:
(member '(2) '((1) (2))) ; |
sequence is a list, this function destructively removes all
elements equal to object from sequence. For lists,
delete is to delq as member is to memq: it
uses equal to compare elements with object, like
member; when it finds an element that matches, it removes the
element just as delq would.
If sequence is a vector or string, delete returns a copy
of sequence with all elements equal to object
removed.
For example:
(delete '(2) '((2) (1) (2)))
=> ((1))
(delete '(2) [(2) (1) (2)])
=> [(1)]
|
delete. If
returns a copy of sequence, a list, vector, or string, with
elements equal to object removed. For example:
(remove '(2) '((2) (1) (2)))
=> ((1))
(remove '(2) [(2) (1) (2)])
=> [(1)]
|
Common Lisp note: The functionsmember,deleteandremovein GNU Emacs Lisp are derived from Maclisp, not Common Lisp. The Common Lisp versions do not useequalto compare elements.
See also the function add-to-list, in 11.8 How to Alter a Variable Value,
for another way to add an element to a list stored in a variable.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
An association list, or alist for short, records a mapping from keys to values. It is a list of cons cells called associations: the CAR of each cons cell is the key, and the CDR is the associated value.(2)
Here is an example of an alist. The key pine is associated with
the value cones; the key oak is associated with
acorns; and the key maple is associated with seeds.
((pine . cones) (oak . acorns) (maple . seeds)) |
The associated values in an alist may be any Lisp objects; so may the
keys. For example, in the following alist, the symbol a is
associated with the number 1, and the string "b" is
associated with the list (2 3), which is the CDR of
the alist element:
((a . 1) ("b" 2 3))
|
Sometimes it is better to design an alist to store the associated value in the CAR of the CDR of the element. Here is an example of such an alist:
((rose red) (lily white) (buttercup yellow)) |
Here we regard red as the value associated with rose. One
advantage of this kind of alist is that you can store other related
information--even a list of other items--in the CDR of the
CDR. One disadvantage is that you cannot use rassq (see
below) to find the element containing a given value. When neither of
these considerations is important, the choice is a matter of taste, as
long as you are consistent about it for any given alist.
Note that the same alist shown above could be regarded as having the
associated value in the CDR of the element; the value associated
with rose would be the list (red).
Association lists are often used to record information that you might otherwise keep on a stack, since new associations may be added easily to the front of the list. When searching an association list for an association with a given key, the first one found is returned, if there is more than one.
In Emacs Lisp, it is not an error if an element of an association list is not a cons cell. The alist search functions simply ignore such elements. Many other versions of Lisp signal errors in such cases.
Note that property lists are similar to association lists in several respects. A property list behaves like an association list in which each key can occur only once. See section 8.4 Property Lists, for a comparison of property lists and association lists.
equal (see section 2.7 Equality Predicates). It returns nil if no
association in alist has a CAR equal to key.
For example:
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
=> ((pine . cones) (oak . acorns) (maple . seeds))
(assoc 'oak trees)
=> (oak . acorns)
(cdr (assoc 'oak trees))
=> acorns
(assoc 'birch trees)
=> nil
|
Here is another example, in which the keys and values are not symbols:
(setq needles-per-cluster
'((2 "Austrian Pine" "Red Pine")
(3 "Pitch Pine")
(5 "White Pine")))
(cdr (assoc 3 needles-per-cluster))
=> ("Pitch Pine")
(cdr (assoc 2 needles-per-cluster))
=> ("Austrian Pine" "Red Pine")
|
The functions assoc-ignore-representation and
assoc-ignore-case are much like assoc except using
compare-strings to do the comparison. See section 4.5 Comparison of Characters and Strings.
nil if no association in alist has
a CDR equal to value.
rassoc is like assoc except that it compares the CDR of
each alist association instead of the CAR. You can think of
this as "reverse assoc", finding the key for a given value.
assoc in that it returns the first
association for key in alist, but it makes the comparison
using eq instead of equal. assq returns nil
if no association in alist has a CAR eq to key.
This function is used more often than assoc, since eq is
faster than equal and most alists use symbols as keys.
See section 2.7 Equality Predicates.
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
=> ((pine . cones) (oak . acorns) (maple . seeds))
(assq 'pine trees)
=> (pine . cones)
|
On the other hand, assq is not usually useful in alists where the
keys may not be symbols:
(setq leaves
'(("simple leaves" . oak)
("compound leaves" . horsechestnut)))
(assq "simple leaves" leaves)
=> nil
(assoc "simple leaves" leaves)
=> ("simple leaves" . oak)
|
nil if no association in alist has
a CDR eq to value.
rassq is like assq except that it compares the CDR of
each alist association instead of the CAR. You can think of
this as "reverse assq", finding the key for a given value.
For example:
(setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
(rassq 'acorns trees)
=> (oak . acorns)
(rassq 'spores trees)
=> nil
|
Note that rassq cannot search for a value stored in the CAR
of the CDR of an element:
(setq colors '((rose red) (lily white) (buttercup yellow)))
(rassq 'white colors)
=> nil
|
In this case, the CDR of the association (lily white) is not
the symbol white, but rather the list (white). This
becomes clearer if the association is written in dotted pair notation:
(lily white) == (lily . (white)) |
string-match with an alist that contains
regular expressions (see section 34.3 Regular Expression Searching). If test is omitted
or nil, equal is used for comparison.
If an alist element matches key by this criterion,
then assoc-default returns a value based on this element.
If the element is a cons, then the value is the element's CDR.
Otherwise, the return value is default.
If no alist element matches key, assoc-default returns
nil.
(setq needles-per-cluster
'((2 . ("Austrian Pine" "Red Pine"))
(3 . ("Pitch Pine"))
(5 . ("White Pine"))))
=>
((2 "Austrian Pine" "Red Pine")
(3 "Pitch Pine")
(5 "White Pine"))
(setq copy (copy-alist needles-per-cluster))
=>
((2 "Austrian Pine" "Red Pine")
(3 "Pitch Pine")
(5 "White Pine"))
(eq needles-per-cluster copy)
=> nil
(equal needles-per-cluster copy)
=> t
(eq (car needles-per-cluster) (car copy))
=> nil
(cdr (car (cdr needles-per-cluster)))
=> ("Pitch Pine")
(eq (cdr (car (cdr needles-per-cluster)))
(cdr (car (cdr copy))))
=> t
|
This example shows how copy-alist makes it possible to change
the associations of one copy without affecting the other:
(setcdr (assq 3 copy) '("Martian Vacuum Pine"))
(cdr (assq 3 needles-per-cluster))
=> ("Pitch Pine")
|
eq to key. It returns alist, modified
in this way. Note that it modifies the original list structure
of alist.
(assq-delete-all 'foo
'((foo 1) (bar 2) (foo 3) (lose 4)))
=> ((bar 2) (lose 4))
|
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
James Thornton, jamesthornton.com>Services: Geomarketing |
Electric Speed: Web Designers |