Many functions build lists, as lists reside at the very heart of Lisp.
cons is the fundamental list-building function; however, it is
interesting to note that list is used more times in the source
code for SXEmacs than cons.
This function is the fundamental function used to build new list structure. It creates a new cons cell, making object1 the car, and object2 the cdr. It then returns the new cons cell. The arguments object1 and object2 may be any Lisp objects, but most often object2 is a list.
(cons 1 '(2)) ⇒ (1 2) (cons 1 '()) ⇒ (1) (cons 1 2) ⇒ (1 . 2)
consis often used to add a single element to the front of a list. This is called consing the element onto the list. For example:(setq list (cons newelt list))Note that there is no conflict between the variable named
listused in this example and the function namedlistdescribed below; any symbol can serve both purposes.
This function creates a list with objects as its elements. The resulting list is always
nil-terminated. If no objects are given, the empty list is returned.(list 1 2 3 4 5) ⇒ (1 2 3 4 5) (list 1 2 '(3 4 5) 'foo) ⇒ (1 2 (3 4 5) foo) (list) ⇒ nil
This function creates a list of length length, in which all the elements have the identical value object. Compare
make-listwithmake-string(see Creating Strings).(make-list 3 'pigs) ⇒ (pigs pigs pigs) (make-list 0 'pigs) ⇒ nil
This function returns a list containing all the elements of sequences. The sequences may be lists, vectors, or strings, but the last one should be a list. All arguments except the last one are copied, so none of them are altered.
More generally, the final argument to
appendmay be any Lisp object. The final argument is not copied or converted; it becomes the cdr of the last cons cell in the new list. If the final argument is itself a list, then its elements become in effect elements of the result list. If the final element is not a list, the result is a “dotted list” since its final cdr is notnilas required in a true list.See
nconcin Rearrangement, for a way to join lists with no copying.Here is an example of using
append:(setq trees '(pine oak)) ⇒ (pine oak) (setq more-trees (append '(maple birch) trees)) ⇒ (maple birch pine oak) trees ⇒ (pine oak) more-trees ⇒ (maple birch pine oak) (eq trees (cdr (cdr more-trees))) ⇒ tYou can see how
appendworks by looking at a box diagram. The variabletreesis set to the list(pine oak)and then the variablemore-treesis set to the list(maple birch pine oak). However, the variabletreescontinues to refer to the original list:more-trees trees | | | ___ ___ ___ ___ -> ___ ___ ___ ___ --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil | | | | | | | | --> maple -->birch --> pine --> oakAn empty sequence contributes nothing to the value returned by
append. As a consequence of this, a finalnilargument forces a copy of the previous argument.trees ⇒ (pine oak) (setq wood (append trees ())) ⇒ (pine oak) wood ⇒ (pine oak) (eq wood trees) ⇒ nilThis once was the usual way to copy a list, before the function
copy-sequencewas invented. See Sequences Arrays Vectors.With the help of
apply, we can append all the lists in a list of lists:(apply 'append '((a b c) nil (x y z) nil)) ⇒ (a b c x y z)If no sequences are given,
nilis returned:(append) ⇒ nilHere are some examples where the final argument is not a list:
(append '(x y) 'z) ⇒ (x y . z) (append '(x y) [z]) ⇒ (x y . [z])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.
The
appendfunction also allows integers as arguments. It converts them to strings of digits, making up the decimal print representation of the integer, and then uses the strings instead of the original integers. Don't use this feature; we plan to eliminate it. If you already use this feature, change your programs now! The proper way to convert an integer to a decimal number in this way is withformat(see Formatting Strings) ornumber-to-string(see String Conversion).