Common Lisp/Basic topics/Lists


 * This module is far from finished. Improvements are welcome.

Lists in common lisp are built up from pairs called cons cells. Each cell has two parts, historically named car (for the first part) and cdr (for the remainder).

Lists are implemented with cons cells by having the cdr of the first cell point to another cons cell.

The empty list is also known as nil, it's written representation is either the literal nil or the empty list:.

You can create a list by using cons to construct a new cons cell with a left and right hand part:

Keep in mind that it is possible to create a cons cell which is not a list:

This is called a dotted list. To be a proper list, the final cdr in the chain of cons cells must point to the empty list (nil). You can also create a list with the function list:

which is equivalent to:

You can access the members of a list with the functions car (or first) and cdr (or rest):

Common Lisp also defines the functions second, third and fourth (on up to tenth). Note that second is not the same as cdr or rest. cdr and rest both return the remaining list after the first element, while second returns the second item in the list:

Lists are sequences, so functions that operate on sequences also operate on lists:

The function nth also allows you to retrieve selected items from a list:

Note that the first item in a list is the 0th. Common Lisp also has several forms (functions and macros) for operating on the contents of lists. mapcar can be used to process each element of a list one at a time to build up a new list:

In that example, we were mapping over a single list so we used a unary function (a function of one argument). The #' is a special way to pass a function as an argument into a function. We will talk more about it in the next chapter. It is also possible to map over more than one list - the function must then take the same number of arguments as there are lists: