Scheme cons car cdr cadr

Structure and Interpretation of Computer Programs

Cons, car, cdr and cadr are list primitives. In the Scheme programming language, data objects are combined with the list structure. Several primitives can be used to access certain elements in lists. This page references the Structure and Interpretation of Computer Programs.

This Scheme article reviews the cons, car, cdr and cadr procedures.

Cons

The primitive procedure cons can be read as "construct"; cons takes two arguments and returns a list constructed from those two arguments. In this capacity, the cons primitive procedure provides a means of combination for the Scheme programming language.

Car

1

The primitive procedure car does not refer to any sort of automobile; instead, its name refers to the "Contents of Address part of Register" on the original Lisp implementation on the IBM 704. Car will return the first element in a list. (Page 85.)

Cdr

Similar to the car primitive, cdr refers to the phrase "Contents of Decrement part of Register." On the original hardware, this referred to the second and subsequent items in a list. Cdr will not return a single item always, but may return a sublist containing all elements except the first. (Page 85 and 100.)

Cadr

There is another primitive procedure mentioned in the book: cadr, which is a nested application of car and cdr. So this would be the "contents of address part of contents of decrement part of register". So first the cdr is evaluated, which returns a sublist of all except the first item. Then, car is evaluated and the first item of that sublist is returned: the second in the list. (Page 100.)

car  = first item
cdr  = second and all subsequent items in list
cadr = second item

Summary

Programming tip

Scheme is an old teaching language for programming students, and is not even widely used at its place of development. However, by understanding the primitives cons, car, cdr, and cadr, we can gain insight into how you can use means of combination to study a programming language.

SICP CAR and CDR Books About Programming
.NET