CAR and CDR
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... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
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. ===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: 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 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
Cite this
"CAR and CDR". 1784867830. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1365751595 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy