Reputation: 239934
Most of my Lisp experience comes from Elisp. As such, I find myself writing Lisp only on occasion. By the time I come back to it, I often forget the difference between car
and cdr
and need to reference the docs to jog my memory.
What kinds of clever mnemonics do you use to remember the difference between Lisp functions that have naming roots stemming from 1954 era computer architectures (car, cdr, cadr, caar, cddr, etc)?
In addition, why haven't the well-named aliases of first
and rest
gained more traction as idiomatic alternatives for car
and cdr
?
Update: I know the actual definitions of car
and cdr
and how they should be pronounced. Mnemonics only please.
Full Disclosure: This question isn't entirely for me. It's to help other Lisp newcomers get past some of the initial hurdles.
Upvotes: 16
Views: 1943
Reputation: 1
The full reference to the book above is ‘Common Lisp A Gentle Introduction to Symbolic Computation’ By David S. Touretzky. It’s available (Nov 2019) from: https://www.cs.cmu.edu/~dst/LispBook/book.pdf
My (very late) suggestion to mnemonics for CAR & CDR is:
Think of the first element of a list obtained by CAR as represented by A but in one of the fonts which looks like a mirror image of D. This character points to (ie the curved surface of the character points to) the ‘C’omentmence of the list. Then think of the rest of the list obtained by CDR as represented by D. The D points to the ‘R’est or remainder at the back of the list.
Because of restrictions on fonts here I will use ‘<|’ to denote the A in the font which is a reflection of ‘D’ about the vertical axis. For symmetry I will denote the D character by ‘|>’.
Regard R = C^-1. Then CARCDR=CAC^-1CDR=CADR etc are represented by C<|RC|>R = C<||>R etc.
Upvotes: 0
Reputation: 1121
The book "A gentle Introduction to Lisp" does a great job of explaining really big ones like
CADDDAADDR
Start from the right side going left(ADDDAADD), so the above is(where ->
is "then"): CDR->CDR->CAR->CAR->CDR->CDR->CDR->CAR
.
The reason why CAR
& CDR
are preffered over firt
and rest
is(I think) because they can be chained to form functions such as the above.
Upvotes: 1
Reputation: 11
The Mnemonics that I use are:
CAR - Copy Alpha position and Return
CDR - Copy Dendrite (tree part - without the root) and Return
I am newly getting back to trying Lisp, but hopefully this fits.
I tend to think of the starting of a list as the alpha or the root position.
If the first position is the root, and I am used to seeing binary trees (which can be represented as a list), then a word related to trees would seem to be in order. Dendrite has the right beginning letter and seems to fit. It represents the last of the tree without the root.
Another take is from Robert Smith:
CAR - "Cell’s Anterior Region"
CDR - "Cell’s Dorsal Region"
"We can get the second part of the cell. Let’s call this part the dorsal region (why not posterior? The meaning of dorsal makes more sense with lists, in that the dorsal region of a list [1,2,3] is the part “near the end”, [2,3], whereas the posterior would just be 3)."
from Lisp has too many parentheses… (…or so they say!) By Robert Smith, on November 7th, 2010
I know this bears no relation to what the acronyms originally were, but even Steve Russell said:
"Because of an unfortunate temporary lapse of inspiration, we couldn't think of any other names for the 2 pointers in a list node than "address" and "decrement", so we called the functions CAR for "Contents of Address of Register" and CDR for "Contents of Decrement of Register".
After several months and giving a few classes in LISP, we realized that "first" and "rest" were better names, and we (John McCarthy, I and some of the rest of the AI Project) tried to get people to use them instead.
Alas, it was too late! We couldn't make it stick at all. So we have CAR and CDR."
The origin of CAR and CDR in LISP
Upvotes: 1
Reputation: 11306
If you don't care about being idiomatic, use first and rest. car and cdr do have the advantage of being able to be composed into combinations like caddr cddr and so on, if you find that useful.
Otherwise, car is first and it's alphabetically first of the two.
Upvotes: 1
Reputation: 4469
I actually seldom see car
and cdr
, much more often I see first
and rest
in the code. So I can't agree that those named haven't gained traction.
Upvotes: 0
Reputation: 2567
This is really lame, but since no one else has suggested anything...
car to me is the thing that drives, so it's first. cdr is the caboose; it comes after.
See, I told you it was lame.
Upvotes: 13
Reputation: 31012
They stand for "Contents of the Address Register" and "Contents of the Decrement Register", terms derived from the IBM 704 machine architecture. Not that that helps you much!
See http://www.iwriteiam.nl/HaCAR_CDR.html
Upvotes: 5
Reputation: 7667
"car" and "cdr", for me at least, are things you just learn, like the sounds for the words "left" and "right".
"first" and "rest" are only mnemonic if the object being deconstructed is a list. If it is an actual cons (i.e., a dotted pair), they don't help.
They stuck because there WASN'T anything else, almost fifty years ago, when LISP was first developed. All the articles, all the books, all the code used CAR and CDR, and everyone got used to them.
Upvotes: 1
Reputation: 14529
I don't have mnemonics for car and cdr. I mean, there are only the two of them, and if you use Lisp at all, it seems to me you would Just Know. (Hell, I don't even use Lisp and I can remember.)
Besides the convenient composition, car and cdr have the following advantages over first and rest: (1) shorter, (2) same length as each other, (3) they appeared earlier.
Upvotes: 2
Reputation: 74440
I have no mnemonics for remembering car/cdr, though they are alphabetical (a
comes before d
, thus car
is first
).
As far as why did they stick (over things like first
and rest
)? A big part is probably just momentum, but the other is what you already wrote. You can easily write composition functions for them:
(caadar ...) -> (car (car (cdr (car ...))))
Upvotes: 12