eintr: Build a list
Build a list
------------
‘cons’ must have a list to attach to.(1) 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’
expressions 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’.
---------- Footnotes ----------
(1) Actually, you can ‘cons’ an element to an atom to produce a
dotted pair. Dotted pairs are not discussed here; see Dotted Pair
Notation (elisp)Dotted Pair Notation.