9 ms·
The Origin of CAR and CDR in Lisp (2005)
- lisper 9y agoI use FST and RST (for FIRST and REST). I like them because they are short, mnemonic, and can be composed like CAR and CDR, e.g. CADADDR = FRFRRST (though I also believe that if you find yourself doing this you're almost certainly doing something wrong).
- thatswrong0 9y agoI think I prefer this as well. I think the importance of the mnemonic aspect can’t be overstated.. even though I did get _used_ to CAR and CDR, I feel like I was never immediately able to comprehend code that used them.. I had to always perform a mental translation as if I were somewhat bad at French or something and didn’t intuitively know the words. It’s the same thing when I see test code that uses foo, bar, and baz as variables / strings instead of something more memorable like apple, banana, and cherry (or preferably something even more relevant to the code being tested). I feel like I have to work harder to understand what the tests are actually testing when I don’t have a good mental association with the variables under test. I get that it’s something programmers just do, but I’m really not a fan. Maybe I’m an odd one though.
- lisper 9y agoAnother possibility is LHS and RHS for Left Hand Side and Right Hand Side (if you want to emphasize the use of a cons cell as a pair rather than a linked list).
- kbp 9y agoDate: 5 March 1980 08:54-EST From: Mark L. Miller <MILLER at MIT-AI> To: Dave.Touretzky at CMU-10A, RWK cc: KMP, HIC, BUG-LISP Re: CAR and CDR Of course, you could rename them to, e.g., "LHS" and "RHS" for "Left Hand Side" and "Right Hand Side". This would address the composition argument in favor of CAR and CDR: CADR -> LRHS (left-hand-side of right-hand-side), CDADADR -> RLRLRHS, and so on. It's easy to provide these as macros. Later, you can explain that the original names are CAR and CDR, and isn't that silly, etc. Regards, Mark
- kazinator 9y agoShows you how resilient car and cdr have been against decades of complaining and bikeshedding. That just reaffirms them.
- lisper 9y agoI would accept that if Lisp were a runaway success, but it's not. It is very nearly a dead language [1]. It is entirely possible that the unwillingness of the Lisp community to give up in this obscure terminology in favor of something more user friendly contributed to its demise. [1] Look at e.g. https://madnight.github.io/githut/ https://madnight.github.io/githut/. The most popular Lisp is Clojure, with a whopping 0.33% market share. Scheme and Common Lisp don't even make it to the top 50.
- kazinator 9y agoCL was #45 in the car quarter of 2017; by cadr, it slipped into the long(er) tail.
- kbp 9y ago> It is entirely possible that the unwillingness of the Lisp community to give up in this obscure terminology in favor of something more user friendly contributed to its demise. What more-user-friendly terminology do you suggest?
- lisper 9y agoDid you not read the upstream comments? FST and RST, or LHS and RHS depending on whether the intent is to access the cons as a linked list or a pair.
- kazinator 9y agoThe D in CDR already corresponds to "dexter-" (right); we just need "L" for "levo-": CLR: cell levo/left reference. CDR: cell dexter reference. Organic chemistry uses these letters and prefixes. E.g. "L-glutamine", "D-glucose" (a.k.a "dextrose"). The H and S (hand side) don't really contribute anything. (Yes, left is side and a hand).
- gugagore 9y agoYou're not the first person I've heard of that doesn't like foo,bar,baz nonce variable names. But it's the first I've heard of someone preferring meaningful but irrelevant words over meaningless words. In any case, I think it makes sense, even if it's not common within programming culture.
- kazinator 9y agoYou'd have to really sneak this under the cadar if I were reviewing code.
- lispm 9y ago> Clojure both Keeps and Breaks Tradition ... When Rich Hickey invented Clojure, he kept the gems of the LISP tradition, but jettison much of the muck. This is why Clojure has first instead of car and rest instead of cdr Rich Hickey did not invent 'first' and 'rest'. LISP has those since the end of the 70s in language standards. From Common Lisp the Language, published 1984, chapter on lists: [Function] first list second list third list fourth list fifth list sixth list seventh list eighth list ninth list tenth list These functions are sometimes convenient for accessing particular elements of a list. first is the same as car, second is the same as cadr, third is the same as caddr, and so on. Note that the ordinal numbering used here is one-origin, as opposed to the zero-origin numbering used by nth: (fifth x) == (nth 4 x) setf may be used with each of these functions to store into the indicated position of a list. [Function] rest list rest means the same as cdr but mnemonically complements first. setf may be used with rest to replace the cdr of a list with a new value. Lisp Machine Lisp had FIRST, SECOND, ... REST1, ..., REST4 at least in 1979. They are documented in the 2nd edition Lisp Machine manual.
- MarkMMullin 9y agoParaphrasing Mr Adams - “The story so far: In the beginning LISP was created, .... and then Guy wrote the Common LISP Bible. This made a lot of people very angry and been widely regarded as a bad move.” - or, Common Lisp - the LISP communities attempt at a non-proliferation treaty with itself. :-)
- kbp 9y ago> > When Rich Hickey invented Clojure, he kept the gems of the LISP tradition, but jettison much of the muck. This is why Clojure has first instead of car and rest instead of cdr > Rich Hickey did not invent 'first' and 'rest'. Rather than the names 'car' and 'cdr', the "muck" that Rich Hickey jettisoned here was conses as a core language feature. Since Lisp already used first and rest for operating on lists, Clojure borrowed those names, and since Clojure doesn't have conses (in the Lisp sense of the term; Clojure has a cons function, but it just adds elements to sequences rather than constructing pairs), it didn't have any need for car and cdr, which operate on conses. It reminds me of a section from Kent Pitman's article 'More Than Words, or: Lambda, the Ultimate Political Party': > Some years ago, when I was first becoming involved with language standards, I did a personal study of languages in the Lisp family to determine whether there was a common core of operators that were present throughout the family with the same name and semantics. Then after going through several basic operators which differ in behaviour or name across dialects, he concludes: > I did find that CONS was present in every Lisp I looked at with pretty much the same meaning, but that seemed to be an isolated case
- CalChris 9y agoThe LISP people should have changed those names way back when. Not 79, 59. McCarthy himself said then that this representation is independent of the IBM 704 computer, or of any other electronic computer [1]. So why stick with the names of 704 registers? Was there a purpose to this? [1] https://courses.engr.illinois.edu/cs421/sp2012/project/mccarthy-lisp.pdf https://courses.engr.illinois.edu/cs421/sp2012/project/mccar...
- lispm 9y agoLisp has both CAR/CDR and FIRST/REST. It's usual programming style to use CAR/CDR when working with CONS trees/graphs and FIRST/REST when working with lists.
- AnimalMuppet 9y agoOK, but isn't CalChris's point still valid? Something other than CAR/CDR would have been a better choice for working with CONS cells, even back in 1959. Do I think it's a big problem? Not really. Are the names less than optimal? Yes.
- kbp 9y ago> Something other than CAR/CDR would have been a better choice for working with CONS cells, even back in 1959. > Are the names less than optimal? Yes. I don't think that's self-evident. The only better alternatives I've ever heard suggested are something like left and right, which I think are probably slightly clearer names for what the functions do, but not significantly so (I don't think "a cons is a thing with a left and right side" is that much more intuitive than "a cons is a thing with two parts called a car and a cdr"). Further, I think in 1959 when most everyone using Lisp was hacking on the implementation itself, names that are mnemonic for what the machine they were actually using is actually doing make sense; I don't think they're clearly sub-optimal now, but I think they were even less so in 1959.
- AnimalMuppet 9y ago
- DonHopkins 9y agoMy favorite pair of silly LISP function names derived from CAR and CDR is RPLACA and RPLACD. Then of course there's the whole family of derived function names CAAR, CADR, CDAR, CADADR, CADDDDR, CAAAAR, etc. I wonder if that's what inspired Oliver Steele's "The Aargh Page": http://osteele.com/words/aargh http://osteele.com/words/aargh http://blog.osteele.com/2005/12/aargh/ http://blog.osteele.com/2005/12/aargh/ Even sillier is LOGO's alternative to CAR and CDR: FIRST and BUTFIRST. As in "BUTFIRST recursion" (giggle). https://en.wikipedia.org/wiki/List_of_MicroWorlds_Logo_commands#butfirst https://en.wikipedia.org/wiki/List_of_MicroWorlds_Logo_comma...
- bitwize 9y ago> Even sillier is LOGO's alternative to CAR and CDR: FIRST and BUTFIRST. As in "BUTFIRST recursion" (giggle). Frosh-level CS classes may introduce linked lists as classes in some OO programming language -- often with the accessor methods getHead() and getTail(). Huh huh huh huh huh. You'd think CAR and CDR, being abstruse acronyms steeped in technical language, would be immune from this sort of unfortunate double entendre, but no: my AI class professor had an Eastern European accent and pronounced CDR like "cooter".
- montrose 9y agoI've programmed a lot in various dialects of Lisp, and I like car and cdr. Using names like first and rest might make a language more attractive to new users, but there is something to be said for designing at least some languages for experienced users rather than new ones, and it doesn't take much experience (a week?) for the words car and cdr to mean the left and right halves of a cons cell to you. And once they do, they're better than first and rest, because 1. They're shorter. 2. They're the same length, which means in practice that a lot of code lines up in a way that's easier to read. 3. They look similar to one another, which telegraphs that they're part of the same set of list access tools. Code that munges lists looks like it munges lists. 4. Their meaning is precise: car just takes the first element of a list. Whereas something called first might reasonably be expected to also give you the first element of a vector, or a string. 5. When you're operating on lists as trees, the names first and rest are actively misleading. The car and cdr are the left and right subtrees, not the first and rest of something.
- jackmott 9y agofst rst
- kazinator 9y agoWhen you see car and cdr in code, it is usually a loud and clear signal that this module is doing something very concrete and explicit, with conses as a structuring material.
- dang 9y agoI agree. The argument against car/cdr reminds me of the adage 'in theory theory and practice are the same and in practice they're different'. car and cdr may have begun as a historical accident but they stuck around because they hold up in practice. As other commenters have pointed out, we could add to your list the classical point that car and cadr are composable, so cadr, cdar, etc. This creates a simple DSL for list manipulation which sometimes is just what's needed.
- kqr 9y ago> When you're operating on lists as trees, the names first and rest are actively misleading. The car and cdr are the left and right subtrees, not the first and rest of something. If by trees you mean 2-ary trees, sure. For any other n, #'first and #'rest are probably the correct generalisation.
- soegaard 9y agoThese days `first` and `rest` is used in Racket to indicate the input is a list. Since `cons` can build non-lists, the traditional `car` and `cdr` are used when working on trees. Well - actually most Racket programmers use pattern matching when working on trees. Note that `first` throws an error in modern Racket when used on a non-list.
- kbp 9y agoAs noted earlier, the names first and rest date from the 70s and were already around in Lisp Machine Lisp. Also, to nitpick: in Racket, first (car) will sometimes throw when used on lists, too, if the list happens to be the empty list (which has been true for decades in Scheme). It's more correct to say that it throws when used on non-pairs/conses (aka atoms).
- TheHTMLCoder 9y agoGet best conversion services at one place. We convert your designed PSDs to HTML, HTML5, WordPress, WooCommerce, Magento, BuddyPress Themes. All our code is hand-written, W3C Validated, Google Page-Load Optimized, Cross-Browser, Clean & Commented. Details Here: http://www.thehtmlcoder.com/ http://www.thehtmlcoder.com/
- kazinator 9y agoTXR is a new Lisp dialect (project started in 2009) with real conses, car and cdr. Plus some little embellishments in that area: This is the TXR Lisp interactive listener of TXR 188. Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet. 1> (defstruct kar nil kar kdr (:method car (me) me.kar) (:method cdr (me) me.kdr)) #<struct-type kar> 2> (new kar kar 1 kdr 2) #S(kar kar 1 kdr 2) 3> (car *2) 1 4> (cdr *2) 2 :) TXR Lisp has the cadr accessors down to depth five (caar to cddddr). This stuff is not "muck" to be jettisoned; it is part of the essence without which we don't have Lisp.
- kazinator 9y agoCAR and CDR can possibly work very well in Japanese, I have discovered: https://www.reddit.com/r/lisp_ja/comments/78i4ws/ https://www.reddit.com/r/lisp_ja/comments/78i4ws/ CAR -> karu -> 借る (to borrow) CDR -> kudaru -> 下る (to descend) "Borrow" the first value/reference, or "descend" to the next cell. The usual readings are カー/クダー (kaa/kudaa); these have to be altered to have a final ル (ru) rather than "aa". The world isn't all English; someone's Anglo-centric quibble about how "cdr" doesn't mean anything means nothing to someone speaking another language. By the way, descend starts with D: C(Ante-, Ascend)R C(Descend)R I myself seem to be carrying the vestiges of a poorly articulated, subconscious connection to anno domini (A.D.).
- Turing_Machine 9y ago"The world isn't all English" But the history of computing largely is (not totally of course, but largely). If we were discussing sushi, or katanas, or bushido, or netsuke, would you complain about the language being "Japanese-centric"? If we were discussing grand opera (or, for that matter, having a technical discussion of almost any kind of music), would you complain about the language being "Italian-centric"?
- kazinator 9y ago> would you complain I'm not complaining that programming languages use words based on English; my point is about English speakers having bikeshedding quibbles about those words that don't mean anything to non-English-speakers. Suppose some Italian, due to some Italian reasons, doesn't like something about the music term dal segno or any other term. Why would I care, know what I mean.
- Turing_Machine 9y agoHmm... if you didn't care, why would you be reading the Italian quibble on a history site written in Italian? I mean, this was quite clearly a page about the history of the terms, right?
- 9y ago
- dang 9y agoUrl changed from http://www.howardism.org/Technical/Clojure/origin-of-car-and-cdr.html http://www.howardism.org/Technical/Clojure/origin-of-car-and..., which points to this, which is far more informative. The link in that previous article is a little weird, since the text says "lost in the mists of time". That of course isn't true; if you read through to Steve Russell's explanation the mists clear up quickly.
- kruhft 9y agoContents of Address Register and Contents of Decriment Register. Two instructions on the original IBM that the first LISP imterpreter was written on. I heard.
- aap_ 9y agoWhy don't you read the linked text to learn that you're wrong? There is no address register and no decrement register on the IBM 704, and no instructions CAR and CDR either. An instruction had an address part and a decrement part. The LISP functions CAR and CDR return the the address part and decrement part of a given register and both were implemented with more than one machine instruction.
- flavio81 9y ago>When Rich Hickey invented Clojure, he kept the gems of the LISP tradition, but jettison much of the muck. That's the myth that Hickey loves to pass around, but is a totally baseless claim. >This is why Clojure has first instead of car and rest instead of cdr. Just as Common Lisp also has "first" and "rest" if one feels like using them instead of "car" and "cdr".
- PyComfy 9y agoi don't like first/rest; car and cdr are about pairs, not about lists which is an implicit type (like strings in C).
- sulam 9y agoTIL that the guy who invented these names, Steve Russell, was the guy who 9 years later mentored Bill Gates and Paul Allen on the PDP-10. History's cool.
- kbp 9y agoThat is cool, but I was wondering if you had a citation for Steve Russell coining the names? I don't believe that's true.
- timonoko 9y agoFor nonenglish speaker KAR and KUDER were of course as good as any other foreign symbols. First derivatives were KA-AR, KADER and KUD-DER (for CAAR, CADR and CDDR).