car and cdr
Table of Contents
car
and cdr
are 2 main list primitives in lisp. In elisp, there are also cadr
-like macros that is the short hand of using them in a row.
Basically, everything in lisp is cons cells, consist of a car
and a cdr
, that is a head and a tail.
cons cell '(1 . 2) |
list '(1 2) |
list of one '(1) |
|
car |
1 |
1 |
1 |
cdr |
2 |
'(2) |
nil |
cadr
-like macros are interpreted as (car (cdr X))
. They are useful to:
- get value from nested list like
'((1))
(caar
) or'(nil (1))
(caadr
=>1
) - discarding some head elements like in
'(nil nil nil nil 1 2)
, (cddddr
=>'(1 2)
) - act like
second
orthird
but cooler and more cryptic (cadr
=second
;caddr
=third
)
Backlinks
(2024 > 04 - April >
)on 04/04/24, created 3 nodes