ArticleslgStudy

science

CAR and CDR

CAR and CDR is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand CAR and CDR rather than just read about it. In short: In computer programming, CAR (car) and CDR (cdr) ( or ) are primitive operations on cons cells (or "non-atomic S-expressions") introduced in the Lisp programming language. A cons cell is composed of two pointers; the car operation extracts the first pointer, and the cdr operation extracts the second.

Key takeaways

  • CAR and CDR belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect CAR and CDR to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of CAR and CDR from memory before moving on to harder problems.

Reference excerpt

In computer programming, CAR (car) and CDR (cdr) ( or ) are primitive operations on cons cells (or "non-atomic S-expressions") introduced in the Lisp programming language. 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 lists (rather than trees 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 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 word length and a 15-bit address space. 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 registers. Indexing was a subtractive process on the 704, hence the value to be loaded into an index register was called a "decrement". 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". Precursors 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:

The 704 assembler macro for cdr was:

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 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) use a singly linked list 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, 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 uses first instead of car and next or rest instead of cdr. Logo, on the other hand, uses first instead of car and butfirst instead of cdr.

References

Notes Russell, Steve. "Writing and Debugging Programs" (PDF). RLE and MIT Computation Center. CSAIL Publications and Digital Archive (Memo). AI Memo, no. 6. Cambridge, Massachusetts. OCLC 35415961. Retrieved July 20, 2017. (To view documents see Help:FTP) Faase, Frans (2006-01-10). "The origin of CAR and CDR in LISP". Graham, Paul (1996). ANSI Common Lisp. Prentice Hall. ISBN 978-0-13-370875-2. Barski, Conrad (2010). Land of Lisp : learn to program in Lisp, one game at a time!. San Francisco, CA: No Starch Press, Inc. ISBN 978-1-59327-281-4. McCarthy, John (1960). "Recursive functions of symbolic expressions and their computation by machine, Part I." (PDF). Communications of the ACM. 3 (4). ACM New York, NY, USA: 184–195. doi:10.1145/367177.367199. S2CID 1489409.

Worked examples

Example 1 — a first encounter with CAR and CDR

Start with the simplest possible case. Write down what CAR and CDR claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to CAR and CDR before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about CAR and CDR ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of CAR and CDR

In research
CAR and CDR appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses CAR and CDR in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
CAR and CDR is common in secondary-school and first-year university syllabi. It links to neighbouring topics Lisp (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for CAR and CDR outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “CAR and CDR” →

How to study CAR and CDR in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what CAR and CDR means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain CAR and CDR out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is CAR and CDR in simple terms?

In computer programming, CAR (car) and CDR (cdr) ( or ) are primitive operations on cons cells (or "non-atomic S-expressions") introduced in the Lisp programming language. A cons cell is composed of two pointers; the car operation extracts the first pointer, and the cdr operation extracts the secon…

Why does CAR and CDR matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study CAR and CDR?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on CAR and CDR.

Tags

  • Lisp (programming language)

Keep exploring