CS 161 Recitation Notes - CAR and CDR

CAR and CDR are two of the most productive operators in Lisp, so you might wonder why they have such enigmatic names. You might expect them to be called something more representative, like first and rest or head and tail. The actual origin of their names is derived from two registers of the original machine that Lisp was developed on, the IBM 704. CAR and CDR are acronyms for "Contents of the Address portion of the Register" and "Contents of the Decrement portion of the Register".

One nice thing that most interpreters of Lisp allow you to do is use a shorthand notation for sequences of CARs and CDRs. Basically, you just use one C, one R, one set of parens, and as many A's and D's as you need, in the right order. here are some examples:

>(car (cdr '(a b c)))
B

>(cadr '(a b c))
B

>(car (cdr (cdr '(a b c))))
C

>(caddr '(a b c))
C

>(cdr (cdr '(a b c)))
(C)

>(cddr '(a b c))
(C)