The Origin of CAR and CDR in Lisp (2005)

8 years ago (iwriteiam.nl)

I'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.

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

    When designing Rust, we quickly learned that the idea of picking short names to satisfy experienced users at the cost of new ones doesn't work in practice. Too many users complain that the language is too hard to learn and ugly; it took years for "Rust is so ugly, it's the resurrection of Perl" to finally stop being a meme. If we had stuck to our guns with short keywords, Rust might have been dead by now.

    Choosing unfamiliar syntax such as car and cdr worked for Lisp because, during the '70s, Lisp as a whole was novel enough to gain a sizable following. It doesn't work today. (And note that, even then, Lisp lost a lot of potential users due to the S-expression based syntax.) I'm firmly in the "first" and "rest" camp, because history has shown that readable languages much more frequently go on to be successful than languages with idiosyncratic syntactic choices.

    • A sometimes overlooked feature of car and cdr is composibility, e.g. caadr, cddr, etc. Though I don't think it is useful to go ten deep as the spec requires if people are reading the code, deep composition may make some sense for machine written code.

      Car and cdr also reflect a sometimes misunderstood aspect of Lisp: lists are an abstraction for sequential memory addresses in the Von Neumann architecture. The sense in which Lisp was designed for functional programming only goes about as far as one can pragmatically throw the lambda calculus. Practically and historically speaking Lisp programming was only slightly less about mutation than any other language. Design for current state of the art (in the 70's, 80's and 90's) is why Common Lisp has destructive versions of everything useful. Heck, Lisp even mutates its source code.

      At the end of the work week, the major language design choice is not so much between car/cdr and first/rest it's between the language having a concept of first/rest and not having one: e.g. Python, Javscript, Go[?], C, and so on.

      Finally, car/cdr is not that much worse than first/rest for all those programmers who are not fluent in English. Naming things is hard mostly because names are arbitrary. Both car/cdr and first/rest require the harder concept of fixed order data...and next makes more sense than first if one applies the stream abstraction.

      8 replies →

    • > car and cdr worked for Lisp because, during the '70s, Lisp as a whole was novel enough to gain a sizable following. It doesn't work today. (And note that, even then, Lisp lost a lot of potential users due to the S-expression based syntax.) I'm firmly in the "first" and "rest" camp

      first and rest are bad names for what car and cdr do, in the general case. If you have "Jenny" mapped to "867-5309" in a dictionary, then getting the "Jenny" part of that binding with first and the "867-5309" part with rest doesn't really make any sense. Similarly, if you have a node in a binary search tree, getting the subtree with the lesser elements with first and the subtree with the greater elements with rest also doesn't make any sense.

      Lisp, even in the 70s, uses first and rest as synonyms for car and cdr for the case where you are operating on a list. first and rest do not make sense as replacements for car and cdr in the general case, however, and if all you want is to have them as synonyms for when they do make sense, then Lisp has been providing that since the 70s.

      6 replies →

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

      A long forgotten five minutes sometime in Y2000 in my case.

  • When 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.

  • I 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.

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

    That may be true in LISP, but it's not true in Clojure (which I assume is a fair paragon for the first/rest camp), where first works just fine on strings and vectors:

      => (first "abc")
      \a
      => (first [1 2 3])
      1
    

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

    Firstly, trees can clearly branch out more than n=2. Then, nth becomes a much cleaner term than "left" and "right" which immediately limit you to 2 cases. But, let's say we're talking about n=2 trees. Consider:

      headmarc.core=> (second [1 2 3])
      2
    

    And, of course, if that still seems unreasonable:

      (def left first)
      (def right second)
    

    ... but maybe what you really want is a zipper, which has everything you're asking for and more: https://clojure.github.io/clojure/clojure.zip-api.html#cloju...

  • > They look similar to one another, which telegraphs that they're part of the same set of list access tools.

    …and probably allows for more typos and misreads during code review or something else. Luckily 'a' is distinct enough from 'd' visually so the difference between them is more noticable; imagine functions ending up with unfortunate names like cbr/cdr or car/cer…

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

CAR and CDR can possibly work very well in Japanese, I have discovered:

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

  • "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"?

    • > ... would you complain about the language being ..

      I'm not sure there was a complaint there. To me, it read more like an interesting side note.

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

      2 replies →

My 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://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_comma...

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

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

  • > > 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

  • Paraphrasing 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. :-)

  • Those who do not understand the history of Clojure are doomed to assert Rich Hickey invented Common Lisp.

  • > Rich Hickey did not invent 'first' and 'rest'.

    No one claimed Hickey invented first and rest, or even implied it. The point is not that first and rest didn't exist, but that car and cdr DID. Clojure deliberately left them out, as a design choice. For good or ill, that's the point.

    • > No one claimed Hickey invented first and rest, or even implied it.

      I think that saying "This is why Clojure has first instead of car and rest instead of cdr" is a very odd way of saying "this is why Clojure doesn't have car and cdr" if you don't mean to imply that it added first and rest.

      I also think the originally linked article was rather poorly written and seemed to put all of the focus on "car and cdr sure are weird names" without examining that they are operations on cons cells, which Clojure doesn't have. Clojure leaving out the names car and cdr doesn't really have anything to do with those names; it left out the data structure they operate on. A language without numbers proably wouldn't have a sqrt function either, but that has little to do with the clarity of the name sqrt.

      Discussing the differences and tradeoffs between how Lisps and Schemes all represent lists compared to how Clojure does (and the tradeoffs with how Clojure makes up for the other things that conses are used for in Lisp) might have made for a more interesting point, but it would have been a harder one to make than just pointing at two not immediately obvious symbol names (without even mentioning that it did keep the equally archaic and unhelpful name "cons" but changed its behaviour).

    • > but that car and cdr DID. Clojure deliberately left them out, as a design choice.

      Clojure doesn't have the concept of Lisp's linked lists, thus it does not have its operators.

      Note that where the link now points to is something different. Originally this was the link: http://www.howardism.org/Technical/Clojure/origin-of-car-and...

      > Clojure deliberately left them out, as a design choice.

      I don't think they were 'left out'. That's not how Clojure was designed, IIRC. Clojure is not first Lisp minus the arcane names, plus second then the new stuff. It's a new language from start, not Lisp with names left out. Hickey did not start with Lisp and redesigned it. He started with a new language, based on ideas like immutable persistent lazy sequences, host language integration with easy interoperation, some Lisp ideas like s-expressions and macros, ... I don't think he thought, 'I leave CAR and CDR out'. There was no place for them, since he designed the language Clojure around different data structure concepts.

I 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).

  • I 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.

    • Another 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).

      9 replies →

    • You'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.

The 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/mccar...

  • Lisp 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.

    • OK, 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.

      6 replies →

  • > So why stick with the names of 704 registers? Was there a purpose to this?

    Are there other, much better names for the two parts of a pair? First and second aren't great because they're not clear that it's a pair and not a larger sequence (and the difference is important in Lisp; since lists are built on conses, it would be odd to get the tail of a list by calling 'second'). The only other names I can think of would be something like left and right, but I don't think those would be substantially easier to understand for beginners than car and cdr.

    When working with lists, many people prefer to use the functions first and rest, which behave identically to car and cdr but are more meaningfully named for list applications. They would be terrible names for the general cons-handling functions, though.

    • Left and Right are really commonly used in this situation in ML-descended languages. It’s not the worst thing.

      I don’t mind car and cdr though. I kind of wish they were in Clojure tbh. It feels nice to pay tribute to a half-century of computing history.

      1 reply →

    • Would it be silly to use 'head' and 'tail' then?

      I learned car/cdr in school so that's what I use.

    • I 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).

    • In C++, I often use LHS and RHS (left hand side / right hand side). I know other people who like fst, eat (first, rest). Both seem better to me, as they have some meaning other than random made up strings (for people who don't use 704).

      6 replies →

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

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

  • Get 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/

  • As 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).

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

For 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).

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

  • That is cool, but I was wondering if you had a citation for Steve Russell coining the names? I don't believe that's true.

i don't like first/rest; car and cdr are about pairs, not about lists which is an implicit type (like strings in C).

Contents of Address Register and Contents of Decriment Register.

Two instructions on the original IBM that the first LISP imterpreter was written on.

I heard.

  • Why 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.