Old question, but I may as well explain why cdr
acts the way it does in case someone Googles the question later...
cdr
acts this way because lists in Lisps like Elisp are not quite lists like how you would imagine them, they are actually made up of things called cons cells.
Essentially, when you make a list, it isn't a simple collection of items, one after another--it's actually just a chain of tiny lists, each only capable of holding two values. These pairs are the "cons cells".
In each pair, the first value holds the value of the list at that point, and the second holds either "nil
" (End of the list), or a reference to another cons cell (The next item in the list).
So:
ELISP> (list 1)
(1)
Is the same as:
ELISP> (cons 1 nil)
(1)
And:
ELISP> (list 1 4)
(1 4)
Is the same as:
ELISP> (cons 1 (cons 4 nil))
(1 4)
...and so forth.
Now, what car
and cdr
do is take the first and second values of these cons cells.
So, looking at your list when expanded to cons cells (That's (cons 1 (cons 4 nil))
):
ELISP> (car (cons 1 (cons 4 nil)))
1
Because the first value of the outer cons
is "1
", and:
ELISP> (cdr (cons 1 (cons 4 nil)))
(4)
Because the second value of the outer cons
is "(cons 4 nil)
" and:
ELISP> (cons 4 nil)
(4)
It is possible for a cdr
to hold something other than nil
or a reference to another cons cell, but these are not the same type of list, but rather a "dotted pair":
ELISP> (cons 1 4)
(1 . 4)
ELISP> (car (cons 1 4))
1
ELISP> (cdr (cons 1 4))
4
Hopefully this clears some things up..!