Scheme, cons. Cons, car, cdr and cadr are list primitives. In Scheme, data objects are combined with the list structure. Primitives are used to access certain elements in lists.
Primitive info. We reference the Structure and Interpretation of Computer Programs. These terms have expanded meanings—so "cons" means "construct."
Cons. As noted, the primitive procedure cons means "construct." Cons takes two arguments and returns a list constructed from those two arguments.
And In this capacity, it provides a means of combination for the Scheme programming language.
Note In programming languages, a constructor creates a new object, such as a list object.
car = first item
cdr = second and all subsequent items in list
cadr = second item
Car. Car 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.
And Cdr will not return a single item always, but may return a sublist containing all elements except the first (pages 85 and 100).
Cadr. This is a nested application of car and cdr. So this would be the "contents of address part of contents of decrement part of register".
And 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).
Summary. In 2022, Scheme is not widely used. But by understanding the primitives cons, car, cdr, and cadr, we can gain insight into how you can use means of combination.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 14, 2022 (edit).