This function returns the value pointed to by the first pointer of the cons cell cons-cell. Expressed another way, this function returns the car of cons-cell.
As a special case, if cons-cell is
nil, thencaris defined to returnnil; therefore, any list is a valid argument forcar. An error is signaled if the argument is not a cons cell ornil.(car '(a b c)) ⇒ a (car '()) ⇒ nil
This function returns the value pointed to by the second pointer of the cons cell cons-cell. Expressed another way, this function returns the cdr of cons-cell.
As a special case, if cons-cell is
nil, thencdris defined to returnnil; therefore, any list is a valid argument forcdr. An error is signaled if the argument is not a cons cell ornil.(cdr '(a b c)) ⇒ (b c) (cdr '()) ⇒ nil
This function lets you take the car of a cons cell while avoiding errors for other data types. It returns the car of object if object is a cons cell,
nilotherwise. This is in contrast tocar, which signals an error if object is not a list.(car-safe object) == (let ((x object)) (if (consp x) (car x) nil))
This function lets you take the cdr of a cons cell while avoiding errors for other data types. It returns the cdr of object if object is a cons cell,
nilotherwise. This is in contrast tocdr, which signals an error if object is not a list.(cdr-safe object) == (let ((x object)) (if (consp x) (cdr x) nil))
This function returns the nth element of list. Elements are numbered starting with zero, so the car of list is element number zero. If the length of list is n or less, the value is
nil.If n is negative,
nthreturns the first element of list.(nth 2 '(1 2 3 4)) ⇒ 3 (nth 10 '(1 2 3 4)) ⇒ nil (nth -3 '(1 2 3 4)) ⇒ 1 (nth n x) == (car (nthcdr n x))
This function returns the nth cdr of list. In other words, it removes the first n links of list and returns what follows.
If n is zero or negative,
nthcdrreturns all of list. If the length of list is n or less,nthcdrreturnsnil.(nthcdr 1 '(1 2 3 4)) ⇒ (2 3 4) (nthcdr 10 '(1 2 3 4)) ⇒ nil (nthcdr -3 '(1 2 3 4)) ⇒ (1 2 3 4)
Many convenience functions are provided to make it easier for you to access particular elements in a nested list. All of these can be rewritten in terms of the functions just described.
Each of these functions is equivalent to one or more applications of
carand/orcdr. For example,(cadr x)is equivalent to
(car (cdr x))and
(cdaddr x)is equivalent to
(cdr (car (cdr (cdr x))))That is to say, read the a's and d's from right to left and apply a
carorcdrfor each a or d found, respectively.
This is equivalent to
(nth 0list), i.e. the first element of list. (Note that this is also equivalent tocar.)
These are equivalent to
(nth 2list)through(nth 9list)respectively, i.e. the third through tenth elements of list.