Hacker News new | past | comments | ask | show | jobs | submit login
CAR and CDR (wikipedia.org)
40 points by sabalaba on Nov 20, 2013 | hide | past | favorite | 46 comments

I always forgot which was which until I actually forced me to use them for a while (and "while" was too long). The only cool thing of the names is that you can get a cryptic sticker "My other car is a cdr" that no-one will understand but will make you jiggle each morning

I feel the same way about many *nix commands. A while ago I thought it was neat to have TLAs[1] for everything, but now I just think it's bad "API" design.

Why is it ls instead of list? chmod instead of permissions? grep instead of search? mv, cp, rm instead of move, copy, remove? Is it really so much work to type a few more characters to justify this?

Programming langue APIs (or rather their libraries) have come a long way since the C days in that regard, but OS wise we're still stuck with these cryptic names.

[1] http://en.wikipedia.org/wiki/Three-letter_acronym

I don't mind that much unix commands, in general (with their names, not with their usage, I hate using grep and find.)

I hate chmod, too, because I can't remember exactly what permissions are what (number-wise.) My fault, not his. But I'd love it to be named "grant" and have a neater language. Like grant user/group/whatever read,write,execute (rwx are actually okayish...) filename. I know chmod is more or less like this. But I don't stand it. It's like combinatoric problems, something deep inside of me refuses to accept doing it :)

I guess you know what grep stands for, don't you? It's a "legacy" command from an early version of ed, g/re/p as in "global search" a "re"gex "p"rinting results. It could be named re (for regex, search implicit) or res (I like this.) Anyway, I use ag (the silver searcher) for text/code search, though. Less letters, easier to use (I keep forgetting if I need -r, -e and all that, ag makes it somewhat easier.)

As for mv, cp, rm, if all 2-letter acronyms were that easy to remember! Did you use extensively DOS? Most commands where far too verbose, I cringe if thinking about having to write copy to copy. Cp is just a smash with the left hand and another with the right hand and off you go. Minimise errors, 2 strokes. Actually now I wonder why in nix we have clear and not cls (or just cl)

As a matter of names... Plan9 uses nix names, and since Plan9 tried to do things "differently" almost just for the sake of it, either they didn't think about it or thought it was a bad idea.

You can use symbolic modes in chmod too if you can't remember the octal representation. For example, you could set 0755 with "u=rwx,go=rx."

Meh, whats one word cloud over another? We can just say the reason for Unix is because: history, and be done with the discussion .. throw a bit of 'its probably because IBM Selectric had easy keys for | and blah' seasoning, douse a fair bit of flamage with bowls of "oh, the monte-carlo on the keypresses says that its all EMACS fault" kerosene, but then again, like I said: whats one word cloud over another? I've heard much relief, for example, from the no-more-Powershell-users who finally 'grok' bash, while (1) grep :P * ; done

*nix commands are optimized for minimizing keystrokes, too.

Would you like to type out 2-3x more characters per command?

Yes, I wouldn't mind. That's what I'm saying. Imho it's a false assumption that you have to "minimize keystrokes". Programming languages noticed this a while ago.

The time I spent thinking "what was that command again?" is far greater than the time I would need to type a few more characters.

> The time I spent thinking "what was that command again?" is far greater than the time I would need to type a few more characters.

For ls? rm? mv?

I use ls many times per day. Lengthening the name would be a huge loss for absolutely no gain; the odds of my forgetting what it's called are precisely zero. The more you use something, the more gain you get from a short name; unix commands are a near-perfect example of where even cryptic names (which mv, cp, rm, and ls do not qualify as) are very desirable.

Commands are not APIs, and an interactive shell is not a programming language. It's an interactive user interface that some of us spend significant portions of our lives dealing with. I no more want to type an API-like "list_files" command ten billion times than I want to have it take ten mouse clicks to launch an application.

PowerShell does that - everything's a long verbose command. It helps avoid clashes in the global namespace. But it's only really usable with autocomplete. Same with Java, C#, and so on. Also, in local scope, long verbose names are far less readable. It might seem more skimmable or easy to get into a codebase with verbose names, but when you're debugging pieces of code, all the extra letters seem to clutter things up.

Sure, t, i, j and f are much more readable than elapsed_time, row_index, col_index and input_file. ;)

As I answered about (so I'm repeating it again :D) minimising keystrokes can also minimise typos (or generate horribly harmful ones, too, agreed)

Why did you type it's instead of it is? Is it really so much work to type one more character to justify this? :-)

The shell is a working language as much as English is. You'll spend more time using it than learning it, so get crackin'.

Mine is "car" and "CarDriveR" (implying first comes the car, then its driver). "CorriDoR" is another one which implies the extra length a bit better.

What's wrong with an ordinal sort? car sorts before cdr.

Good point!

I just hard-wired into my brain they are car and cdr (I read it as "cadar," with both a's quite like the a in car, Spanish a for me.) In a cons list, you have the car (first) and then the cadar. Think it enough times and it sinks eventually.

But "cadar" is also a thing: it's the car of the cdr of the car.

I know, I'm no lisp beginner, but the pronunciation of cdr is almost like "cadar". I just "Spanishised" it.

Still prefer first and rest. At least that has meaning outside the first use of car and cdr.

I can't disagree with that, but after learning clojure I realized that I interned `car` and `cdr` as symbols of which I like :

  - the symmetry: 'c[ad]r'
  - thus same size
  - small size (3)
and ended up being annoyed by `first` and `rest`. I see them as APL-like characters now.

ps: based on that, head/tail, hd/tl, fst/snd (seen in ml dialects) are also good candidates.

I'll take head/tail over first/rest, but first/rest is not too bad.

I thought that for a bit until I realised tail is a bit ambiguous as it's used in the Unix space to mean the "last n" items and in some data structures as the last item pointer.

Ironically in CL LAST does what UNIX tail does.

Yeah which is where SBCL and me got into to a bad argument :)

You probably were looking for LAST1 (e.g. (first (last ...))).

But why SBCL? LAST should behave the same on all implementations.

That's just the variety of CL that I used.

I'm a fan first/rest because of Mathematica. It has four functions that all end in "st", which means I can always recall the one I need:

First, Rest, Most, Last

(Results on Range[4], respectively: 1, {2, 3, 4}, {1, 2, 3}, 4.)

Rebol comes with first & last but not the others.

  >> a: [1 2 3 4]
  == [1 2 3 4]

  >> first a
  == 1

  >> last a
  == 4
This is how you could add rest & most:

  >> rest: func [s] [next s] 
  >> most: func [s] [copy/part s back tail s]

  >> rest a
  == [2 3 4]

  >> most a                                  
  == [1 2 3]
Rebol unifies all this under what it calls a series - http://www.rebol.com/docs/core23/rebolcore-6.html

In Clojure it's almost the same: first, rest, butlast, last

It has nothing to do with preference. FIRST and REST are to be used when working on a LIST, CAR and CDR when working on CONS cells.

Yes but every structure that a cons represents can be expressed in terms of first and rest. That includes tables, singly linked lists, doubly linked lists, binary trees from experience.

Car and Cdr are arbitrary and legacy terms.

Arbitrary and legacy, sure, but they do serve their purpose by communicating different expectations when using a single operator. CDR? result can be anything. REST? author probably thinks the result will always be a LIST. I can think of other examples using SETF but I think I made my point clear.

Same here, but cons cells can be used to model things other than lists, where semantically first and rest don't make sense.

I once wrote an in-place quicksort in common lisp where I kept pointers to locations of interest in the list as halves of a cons cell. Was kind of fun coming across it later and figuring out what it did. That cons cell made no semantic sense as a linked list; it was just a pair of pointers.

They are better, until you try to compose them :)

but saying "cudder" is so much more fun

Yes but macros like caaaaaar and caaadr aren't :)

I'd rather salt my eyeballs.

Such a horror to see this in a large code base. s-expressions can get mentally taxing as it is, but then the abuse of continued acceptance is sad.

It's a pity they're not standard in many other languages. I'm not a fan of having to type first/rest or playing around with syntax soup and I really miss caddadadaddaaaaaring around.

sub car { $_[0] } sub cdr { shift; @_ } # every utils module ever

Plonk that into an autobox module (like perl5i and/or autobox::Core) and enjoying even more caddadadaddaaaaaring around!

For eg:

  use perl5i::2;

  sub autobox::Core::ARRAY::car { $_[0]->[0] }

  sub autobox::Core::ARRAY::cdr {
      my $last = $#{$_[0]};
      wantarray ? @{$_[0]}[1 .. $last] : [@{$_[0]}[1 .. $last]];

  my @a = 1..4;

  @a->car;             # 1
  @a->cdr->cdr->car;   # 3
  @a->cdr;             # 2, 3, 4

  my @x = @a->cdr;
  @x->cdr;             # 3, 4
Some refs: https://metacpan.org/pod/perl5i | https://metacpan.org/pod/autobox::Core


Perl can be written ugly enough without introducing car and cdr to it!

I first learned Lisp so long ago that car and cdr are still ingrained in my head. first and last feel somehow wrong, to me.

quote used to mark data as opposed to code can be tricky while using CDR & CAR : http://stackoverflow.com/questions/18697105/cdadr-on-nested-...

:* <3 u LISP

I like them because they don't guide thought: They're abstract names for abstract concepts. They don't impose any interpretation on the ideas, they simply present them for what they are. Having to break down a less-useful conceptual model is even more difficult than building a more-useful one.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact