Node:nth, Next:setcar, Previous:nthcdr, Up:car cdr & cons
nthcdr function takes the CDR of a list repeatedly.
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)))
nth was defined in Emacs Lisp in
but its definition was redone in C in the 1980s.)
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'.
(nth 0 '("one" "two" "three")) => "one" (nth 1 '("one" "two" "three")) => "two"
It is worth mentioning that
cdr, does not change the original list--the function is
non-destructive. This is in sharp contrast to the