Skip to main content

first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth

first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth Accessor

Syntax:

first list → object

second list → object

third list → object

fourth list → object

fifth list → object

sixth list → object

seventh list → object

eighth list → object

ninth list → object

tenth list → object

Arguments and Values:

(setf (first list**)** new-object**) (setf (second** list**)** new-object**) (setf (third** list**)** new-object**) (setf (fourth** list**)** new-object**) (setf (fifth** list**)** new-object**) (setf (sixth** list**)** new-object**) (setf (seventh** list**)** new-object**) (setf (eighth** list**)** new-object**) (setf (ninth** list**)** new-object**) (setf (tenth** list**)** new-object**)**

list—a list, which might be a dotted list or a circular list.

first, second, third, fourth, fifth, sixth, seventh, . . .

object, new-object—an object.

Description:

The functions first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth access the first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth elements of list, respectively. Specifically,

(first list) (car list)

(second list) (car (cdr list))

(third list) (car (cddr list))

(fourth list) (car (cdddr list))

(fifth list) (car (cddddr list))

(sixth list) (car (cdr (cddddr list)))

(seventh list) (car (cddr (cddddr list)))

(eighth list) (car (cdddr (cddddr list)))

(ninth list) (car (cddddr (cddddr list)))

(tenth list) (car (cdr (cddddr (cddddr list))))

setf can also be used with any of these functions to change an existing component. The same equivalences apply. For example:

(setf (fifth list) new-object) (setf (car (cddddr list)) new-object)

Examples:

(setq lst ’(1 2 3 (4 5 6) ((V)) vi 7 8 9 10)) 
(1 2 3 (4 5 6) ((V)) VI 7 8 9 10)
(first lst)1
(tenth lst)10
(fifth lst)((V))
(second (fourth lst))5
(sixth(1 2 3)) → NIL
(setf (fourth lst) "four")"four"
lst → (1 2 3 "four" ((V)) VI 7 8 9 10)

See Also:

car, nth

Notes:

first is functionally equivalent to car, second is functionally equivalent to cadr, third is functionally equivalent to caddr, and fourth is functionally equivalent to cadddr.

The ordinal numbering used here is one-origin, as opposed to the zero-origin numbering used by nth:

(fifth x) (nth 4 x)

Expanded Reference: first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth

Accessing list elements by ordinal position

These accessors provide readable, English-named alternatives to car/cdr compositions. They use one-based indexing.

(first '(a b c d e))
=> A

(second '(a b c d e))
=> B

(third '(a b c d e))
=> C

(fourth '(a b c d e))
=> D

(fifth '(a b c d e))
=> E

Accessing positions beyond the list length

If the list is shorter than the requested position, NIL is returned.

(sixth '(a b c))
=> NIL

(tenth '(1 2 3))
=> NIL

All ten accessors

(let ((lst '(1 2 3 4 5 6 7 8 9 10)))
(list (first lst) (second lst) (third lst)
(fourth lst) (fifth lst) (sixth lst)
(seventh lst) (eighth lst) (ninth lst)
(tenth lst)))
=> (1 2 3 4 5 6 7 8 9 10)

Using setf with positional accessors

All of these accessors are setf-able, allowing modification of list elements by position.

(let ((lst (list 'a 'b 'c 'd 'e)))
(setf (third lst) 'C-PRIME)
lst)
=> (A B C-PRIME D E)

Equivalence with car/cdr and nth

first is equivalent to car, second to cadr, and so on. They also correspond to nth with a zero-based index.

(let ((lst '(10 20 30 40 50)))
(values (first lst)
(car lst)
(nth 0 lst)))
=> 10
=> 10
=> 10

(let ((lst '(10 20 30 40 50)))
(values (third lst)
(caddr lst)
(nth 2 lst)))
=> 30
=> 30
=> 30

Working with nested lists

These accessors can be combined with each other for structured data access.

(defvar *students* '(("Alice" 95) ("Bob" 87) ("Carol" 92)))

(first (first *students*))
=> "Alice"

(second (second *students*))
=> 87

(first (third *students*))
=> "Carol"