HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, CAR (car) and CDR (cdr) ( or ) are primitive operations on
cons In computer programming, ( or ) is a fundamental function in most dialects of the Lisp programming language. ''constructs'' memory objects which hold two values or pointers to two values. These objects are referred to as (cons) cells, conses, ...
cells (or "non-atomic
S-expression In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested List (computing), list (Tree (data structure), tree-structured) data. S-expressions were invented ...
s") introduced in the
Lisp programming language Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
. A cons cell is composed of two pointers; the ''car'' operation extracts the first pointer, and the ''cdr'' operation extracts the second. Thus, the expression (car (cons ''x'' ''y'')) evaluates to ''x'', and (cdr (cons ''x'' ''y'')) evaluates to ''y''. When cons cells are used to implement
singly linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whic ...
s (rather than
trees In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only p ...
and other more complicated structures), the ''car'' operation returns the ''first'' element of the list, while ''cdr'' returns the ''rest'' of the list. For this reason, the operations are sometimes given the names ''first'' and ''rest'' or ''head'' and ''tail''.


Etymology

Lisp was originally implemented on the
IBM 704 The IBM 704 is the model name of a large digital computer, digital mainframe computer introduced by IBM in 1954. Designed by John Backus and Gene Amdahl, it was the first mass-produced computer with hardware for floating-point arithmetic. The I ...
computer, in the late 1950s. The popular explanation that ''CAR'' and ''CDR'' stand for "Contents of the Address Register" and "Contents of the Decrement Register" does not quite match the IBM 704 architecture; the IBM 704 does not have a programmer-accessible address register and the three address modification registers are called "index registers" by IBM. The 704 and its successors have a
36-bit 36-bit computers were popular in the early mainframe computer era from the 1950s through the early 1970s. Starting in the 1960s, but especially the 1970s, the introduction of 7-bit ASCII and 8-bit EBCDIC led to the move to machines using 8-bit ...
word A word is a basic element of language that carries semantics, meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no consensus among linguist ...
length and a 15-bit
address space In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity. For software programs to save and retrieve ...
. These computers had two instruction formats, one of which, the Type A, had a short, 3-bit, operation code prefix and two 15-bit fields separated by a 3-bit tag. The first 15-bit field was the operand address and the second held a decrement or count. The tag specified one of three
index register An index register in a computer's central processing unit, CPU is a processor register (or an assigned memory location) used for pointing to operand addresses during the run of a program. It is useful for stepping through String (computer science ...
s. Indexing was a subtractive process on the 704, hence the value to be loaded into an index register was called a "decrement".704 - electronic data-processing machin
http://bitsavers.informatik.uni-stuttgart.de/pdf/ibm/704/24-6661-2_704_Manual_1955.pdf
/ref> The 704 hardware had special instructions for accessing the address and decrement fields in a word. As a result it was efficient to use those two fields to store within a single word the two pointers needed for a list. Thus, "CAR" is "Contents of the Address ''part'' of the Register". The term "register" in this context refers to "memory location"., page 36, describes cons cells as words with 15-bit "address" and "decrement" fields. PrecursorsA Fortran-Compiled List-Processing Language
/ref>
/ref> to Lisp included functions: *car ("contents of the address part of register number"), *cdr ("contents of the decrement part of register number"), *cpr ("contents of the prefix part of register number"), and *ctr ("contents of the tag part of register number"), each of which took a machine address as an argument, loaded the corresponding word from memory, and extracted the appropriate bits.


704 macros

The 704 assembler macro for car was: LXD JLOC 4 # C( Decrement of JLOC ) → C( C ) # Loads the Decrement of location JLOC into Index Register C CLA 0,4 # C( 0 - C( C ) ) → C( AC ) # The AC register receives the start address of the list PAX 0,4 # C( Address of AC ) → C( C ) # Loads the Address of AC into Index Register C PXD 0,4 # C( C ) → C( Decrement of AC ) # Clears AC and loads Index Register C into the Decrement of AC The 704 assembler macro for cdr was:Portions from NILS' LISP PAGES
http://t3x.dyndns.org/LISP/QA/carcdr.html
/ref>MIT AI Lab Memo
https://web.archive.org/web/20170706114352/ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-006.pdf
/ref>CODING for the MIT-IBM 704 COMPUTE
http://bitsavers.informatik.uni-stuttgart.de/pdf/mit/computer_center/Coding_for_the_MIT-IBM_704_Computer_Oct57.pdf
/ref> LXD JLOC 4 # C( Decrement of JLOC ) → C( C ) # Loads the Decrement of location JLOC into Index Register C CLA 0,4 # C( 0 - C( C ) ) → C( AC ) # The AC register receives the start address of the list PDX 0,4 # C( Decrement of AC ) → C( C ) # Loads the Decrement of AC into Index Register C PXD 0,4 # C( C ) → C( Decrement of AC ) # Clears AC and loads Index Register C into the Decrement of AC A machine word could be reassembled by ''cons'', which took four arguments (''a'',''d'',''p'',''t''). The prefix and tag parts were dropped in the early stages of Lisp's design, leaving CAR, CDR, and a two-argument CONS.


Compositions

Compositions of car and cdr can be given short and more or less pronounceable names of the same form. In Lisp, (cadr '(1 2 3)) is the equivalent of (car (cdr '(1 2 3))); its value is 2. Similarly, (caar '((1 2) (3 4))) (pronounced ) is the same as (car (car '((1 2) (3 4)))); its value is 1. Most Lisps, for example
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
and Scheme, systematically define all variations of two to four compositions of car and cdr.


Other computer languages

Many languages (particularly functional languages and languages influenced by the functional
paradigm In science and philosophy, a paradigm ( ) is a distinct set of concepts or thought patterns, including theories, research methods, postulates, and standards for what constitute legitimate contributions to a field. The word ''paradigm'' is Ancient ...
) use a
singly linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whic ...
as a basic data structure, and provide primitives or functions similar to car and cdr. These are named variously first and rest, head and tail, etc. In Lisp, however, the cons cell is not used only to build linked lists but also to build pair and nested pair structures, i.e. the cdr of a cons cell need not be a list. In this case, most other languages provide different primitives as they typically distinguish pair structures from list structures either typefully or semantically. Particularly in typed languages, lists, pairs, and trees will all have different accessor functions with different type signatures: in
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, for example, car and cdr become fst and snd when dealing with a pair type. Exact analogues of car and cdr are thus rare in other languages.
Clojure Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
uses first instead of car and next or rest instead of cdr.
Logo A logo (abbreviation of logotype; ) is a graphic mark, emblem, or symbol used to aid and promote public identification and recognition. It may be of an abstract or figurative design or include the text of the name that it represents, as in ...
, on the other hand, uses first instead of car and butfirst instead of cdr.


References

;Notes * * * * * {{cite journal , last=McCarthy , first=John , author-link=John McCarthy (computer scientist), title=Recursive functions of symbolic expressions and their computation by machine, Part I., journal=Communications of the ACM, volume=3, issue=4, year=1960, pages=184–195, url=http://jmc.stanford.edu/articles/recursive/recursive.pdf, publisher=ACM New York, NY, USA, doi=10.1145/367177.367199, s2cid=1489409 Lisp (programming language)