CAR and CDR

From Wikipedia, the free encyclopedia

(Redirected from Car (function))
Jump to: navigation, search

Introduced in the Lisp programming language, car (pronounced /ˈkɑr/, just like the English word "car") and cdr (/ˈkʌdər/ or /ˈkʊdər/) are primitive operations upon linked lists composed of cons cells. 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.

Lisp was originally implemented on the IBM 704 computer, in the late 1950s. On the 704, a cons cell was represented by a single 36-bit machine word containing two parts: an address part and a decrement part, each 15 bits long. The assembly functions used to extract either part of a machine word were called car (Contents of Address of Register) and cdr (Contents of Decrement of Register).

The 704 assembler macro for cdr was

LXD JLOC,4
CLA 0,4
PDX 0,4
PXD 0,4[1]

The terms first and rest are common modern alternatives due to their greater clarity. However, the words car and cdr have an advantage over these synonyms: they can be composed. In Lisp, (cadr '(1 2 3)) is the equivalent of (car (cdr '(1 2 3)); its value is 2 (the first item of the rest of (1 2 3). Similarly, (caar '((1 2) (3 4))) is the same as (car (car '((1 2) (3 4)))); its value is 1. Most Lisps set a limit on the number of composed forms they support; Common Lisp and Scheme both provide forms with up to four repetitions of the a and d. While it's not clear that a complex composed form such as (cadadr ...) is any easier for the untrained eye to parse than the spelled out equivalent (car (cdr (car (cdr ...)))) these composed forms can be convenient in certain idiomatic uses such as taking apart lists representing code. For instance given a lambda expression like the following:

 (lambda (a b) (one-thing) (another-thing))

an experienced Lisper would read:

 (let ((p1 (caadr form))
       (p2 (cadadr form))
       (body (cddr form))
   ...)

as extracting the first and second parameters and then the list containing the body of the lambda expression. However this kind of "list destructuring" is common enough, especially when writing macros, that modern Lisps such as Common Lisp provide facilities for doing it directly. That code would be written in Common Lisp using the destructuring-bind macro like this:

 (destructuring-bind ((p1 p2) &body body) (rest form)
   ...)

  1. ^ Portions from NILS' LISP PAGES- http://t3x.dyndns.org/LISP/QA/carcdr.html
Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.