cons
The cons function constructs lists; it is the inverse of
car and cdr. For example, cons can be used to make
a four element list from the three element list, (fir oak maple):
(cons 'pine '(fir oak maple))
After evaluating this list, you will see
(pine fir oak maple)
appear in the echo area. cons puts a new element at the
beginning of a list; it attaches or pushes elements onto the list.
cons must have a list to attach to.(2) You
cannot start from absolutely nothing. If you are building a list, you
need to provide at least an empty list at the beginning. Here is a
series of cons's that build up a list of flowers. If you are
reading this in Info in GNU Emacs, you can evaluate each of the
expressions in the usual way; the value is printed in this text after
`=>', which you may read as `evaluates to'.
(cons 'buttercup ())
=> (buttercup)
(cons 'daisy '(buttercup))
=> (daisy buttercup)
(cons 'violet '(daisy buttercup))
=> (violet daisy buttercup)
(cons 'rose '(violet daisy buttercup))
=> (rose violet daisy buttercup)
In the first example, the empty list is shown as () and a list
made up of buttercup followed by the empty list is constructed.
As you can see, the empty list is not shown in the list that was
constructed. All that you see is (buttercup). The empty list is
not counted as an element of a list because there is nothing in an empty
list. Generally speaking, an empty list is invisible.
The second example, (cons 'daisy '(buttercup)) constructs a new,
two element list by putting daisy in front of buttercup;
and the third example constructs a three element list by putting
violet in front of daisy and buttercup.
Go to the first, previous, next, last section, table of contents.