views:

423

answers:

8

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.

+6  A: 

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 ...))))
Trey Jackson
I think in the SICP videos, they also mention another non-obvious advantage: you can say "`cadaddr`" over the phone, and it is immediately understandable, unambiguous and very efficient. If you were instead saying "`car` of `cdr` of `car` of `cdr` of `cdr`" or even "`first` of `rest` of `first` of `rest` of `rest`", that would be much slower, much harder to understand correctly, and would probably require the other person writing it down synchronously while you are saying it. OTOH, there is nothing stopping you from *saying* `caadar` and the other person *writing* `(car (car (cdr (car …))))`
Jörg W Mittag
*aargh* I screwed up. I meant: there is nothing stopping you from *saying* `caadar` and the other person *writing* `(head (head (tail (head …))))` or `(first (first (rest (first …))))`
Jörg W Mittag
Thanks. Alphabetical is what I've considered too.
Ryan McGeary
If somebody said "cadaddr" to me over the telephone, I'd worry that they were choking on something.
Pillsy
I don't think that names like caddadar are a really good thing. I think that they are only used when lists are used to emulate other data structures - which is wrong.
dmitry_vk
@dmitry-vk Lisp is all about lists, it's pretty natural to use them as data structures. Using structs, in C/C++ (for example), seems pretty awkward to me (though I've been using them for 15 years). To each his own.
Trey Jackson
+1  A: 

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.

John Y
+1  A: 

"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.

John R. Strohm
In Elisp `first` and `rest` are only aliases for `car` and `cdr` defined in `cl` package.
Török Gábor
+2  A: 

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

Jim Ferrans
This isn't really a mnemonic because there's nothing mnemonic about address=first and decrement=last.
Dominic Cooney
+6  A: 

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.

kajaco
At least you suggested something. That's more of what I was looking for.
Ryan McGeary
A: 

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.

dmitry_vk
Clojure doesn't even *have* `car` and `cdr` anymore.
Jörg W Mittag
Interesting. Good info. `car` and `cdr` do appear more prevalent in the Elisp community though. Probably due to all the old packages.
Ryan McGeary
A: 

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.

justinhj
+2  A: 

Of the two words car and cdr, car is the one I've heard first.

Eisen
There's a mnemonic!
Ryan McGeary