Node:nth, Next:setcar, Previous:nthcdr, Up:car cdr & cons
nthThe nthcdr function takes the CDR of a list repeatedly.
The nth function takes the CAR of the result returned by
nthcdr. It returns the Nth element of the list.
Thus, if it were not defined in C for speed, the definition of
nth would be:
(defun nth (n list) "Returns the Nth element of LIST. N counts from zero. If LIST is not that long, nil is returned." (car (nthcdr n list)))
(Originally, nth was defined in Emacs Lisp in subr.el,
but its definition was redone in C in the 1980s.)
The nth function returns a single element of a list.
This can be very convenient.
Note that the elements are numbered from zero, not one. That is to say, the first element of a list, its CAR is the zeroth element. This is called `zero-based' counting and often bothers people who are accustomed to the first element in a list being number one, which is `one-based'.
For example:
(nth 0 '("one" "two" "three"))
=> "one"
(nth 1 '("one" "two" "three"))
=> "two"
It is worth mentioning that nth, like nthcdr and
cdr, does not change the original list--the function is
non-destructive. This is in sharp contrast to the setcar and
setcdr functions.