raganwald
Wednesday, July 19, 2006
  car and cdr have had their fifteen minutes of fame
In response to Six Things I Dislike About Ruby (and Four I Don't), someone quipped:
while we are stamping out those silly ungrammatical C++-isms like "puts", can we also get rid of those silly lispisms of 'car' and 'cdr'?
Yeah, sister (or brother, as the case may be)!

car and cdr are historical anachronisms. They are like secret handshakes: their only remaining value is to help a small cadre of enthusiasts bond around a shared collection of obscure trivia.

The original metacircular

I wouldn't be suprised if Lispers have droppped these from production systems in favour of the logo-isms first and rest. But then again, I wouldn't be surprised if some Lispers hang onto them either. After all, if every textbook uses them as examples, they become familiar through repetition, even if their names no longer make any sense whatsoever.

The last time I checked nobody was running Lisp on IBM 704s any more. These names really deserve a quiet retirement in a shady home where they can share war stories with puts while they listen to goto complain about what happened when call/cc made him obsolete.

All that being said, car and cdr were rather prolific parents: Lisp implementations usually support a special syntactic sugar called composition where you can make up a function with the following form: (/c[ad][ad]*r/ ...) and this is executed as if you types a series of nested cars and cdrs. For example, (caddr foo) has the same effect as (car (cdr (cdr foo))).

I really like composition. We can sort of get that with dot notation and messages in languages like Ruby. But I secretly admire the way the German language lets you compose entirely new nouns by sticking smaller nouns together.

I think car and cdr's composition has a spiritual descendant in Ruby's metaprogramming style. For example, dynamic finders in Rails. Person.find_all_by_username_and_password really feels like caddaddr. Uh, maybe not.

But it's nice to see that some of the good ideas from Lisp thrive in new homes.

Labels:

 

Comments on “car and cdr have had their fifteen minutes of fame:
Dude, that picture is totally goofed in Firefox. Do you only use Safari?
 
that picture is totally goofed

Shrug.

I use Firefox on OS X and I just tried it on Windows XP.

It is hosted on flickr.com... Perhaps there is an issue there when you tried to view it?

How many programmers does it take to change a lightbulb?

The light works in development...

 
I'll go along with getting rid of car and cdr in Lisp if we can agree to get rid of C++ and Perl at the same time.
 
first and rest make perfect sense when you are working with lists, but cons cells can be used to build many other data structures. They are basic building blocks and as such they need an abstract interface, one that does not restrict your thinking to any particular application. In that context car and cdr work much better that first and rest.
 
cons cells can be used to build many other data structures

Sure. But look at any non-Lisp discussion of data structures. You won't see the words car or cdr, would you?

What's wrong with left and right? For that matter, why is a cell containing two pointers magic? Why isn't a cons cell simply a special case of an array?

I think cons cells are only magical on architectures where you can squeeze them both into a single machine word, or on the 704 where they were in separate registers but there was special efficiency loading them both at the same time. And even there, why let the implementation leak into the abstraction?

Given how clever programmers are, why not build a language where everything is an array, but the implementation special cases arrays with two elements into cons cells?

Isn't that what compilers are supposed to do?
 
What's wrong with left and right?

They differ in length, which may preclude elegant formatting of code in some cases. And they are longer, and for something that elementary shorter is better.
And one couldn't write caadr.

For fundamental language contructs, being convenient to use is more important than being easy to remember. In natural languages verbs like to be or to have tend to be irregular.

lt and rt could be great, one could even write llrt, but I'm not sure if it would be such a big win.

Given how clever programmers are, why not build a language where everything is an array, but the implementation special cases arrays with two elements into cons cells?

Of course! I wholeheartedly agree. But that would hardly be Lisp anymore, would it?
 
I like caadr, that's a nice feature.

The next time I'm myself writing some tree navigating methods, I promise to over-reduce things a follows:

.l and .r will be aliases for left and right. Furthermore, any message consisting entirely of ls and rs (such as llr) shall be treated recursively by applying the first letter and passing the rest of the letters as a message to the result.
 
Aah. Here's why car and cdr are better than first and rest:

You can do something like caaaddr and everyone know what that means. Try doing that with first and rest!
 




<< Home
Reg Braithwaite


Recent Writing
Homoiconic Technical Writing / raganwald.posterous.com

Books
What I‘ve Learned From Failure / Kestrels, Quirky Birds, and Hopeless Egocentricity

Share
rewrite_rails / andand / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo

History
06/04 / 07/04 / 08/04 / 09/04 / 10/04 / 11/04 / 12/04 / 01/05 / 02/05 / 03/05 / 04/05 / 06/05 / 07/05 / 08/05 / 09/05 / 10/05 / 11/05 / 01/06 / 02/06 / 03/06 / 04/06 / 05/06 / 06/06 / 07/06 / 08/06 / 09/06 / 10/06 / 11/06 / 12/06 / 01/07 / 02/07 / 03/07 / 04/07 / 05/07 / 06/07 / 07/07 / 08/07 / 09/07 / 10/07 / 11/07 / 12/07 / 01/08 / 02/08 / 03/08 / 04/08 / 05/08 / 06/08 / 07/08 /