Python syntax in Lisp and Scheme

Peter Seibel peter at javamonkey.com
Thu Oct 9 09:56:51 EDT 2003


Pascal Costanza <costanza at web.de> writes:

> As you have already noted in another note, car and cdr can be
> composed. cadr is the second element, caddr is the third, cadddr is
> the fourth, and so on. cddr is the rest after the second element,
> cdddr is the rest after the third element, and so on. Other
> abbreviations I have used relatively often are caar, cdar, cadar.
> 
> These abbreviations seem strange to a Lisp outsider, but they are
> very convenient, and they are easy to read once you have gotten used
> to them. You don't actually "count" the elements in your head every
> time you see these operators, but they rather become patterns that
> you recognize in one go.

As a follow-on to Pascal's point: It might seem, if one just thinks
about function calls that the benefit of the composed C[AD]*R
operations is fairly small, and perhaps not worth the "cost" of being
more cryptic: i.e. Is the savings of a few characters in (cddr foo) vs
(rest (rest foo)) that big a benefit? But since Lisp supports higher
order functions, having single name for those composite functions
saves the clutter of having to create a lambda expression. For
instance, compare:

  (loop for (x y) on list by #'cddr do (foo x y))

vs 

  (loop for (x y) on list by #'(lambda (l) (rest (rest l))) do (foo xy))


I figured this out by deciding--as a matter of style--that I was just
going to use FIRST/REST all the time and then noticing that in
situations like this, CDDR is much more convenient. The point being,
it's hard to forsee all the subtle ways different features interact.
So it can be simultaneously true that CAR and CDR were originally
choosen as names for pretty much arbitrary historical reasons *and*
that they have persisted for a lot of "hard-headed" but subtle
engineering reasons. (Or maybe soft-headed, aesthetic reasons, if you
care to draw the distinction when talking about programming language
design which I don't.)

-Peter

-- 
Peter Seibel                                      peter at javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp




More information about the Python-list mailing list