As mentioned in the preceding section conses can serve as fundamental
building blocks of lists, association lists, dotted lists, trees and
sets. A single cons is created with the cons
function.
cons x y | Function |
The cons
function returns a new cons whose car is x and
cdr is y. For example:
cons(S(a), S(b)) => (a . b) cons(S(a), list(S(b), S(c), S(d))) => (a b c d)
The accessors of a cons are the car
and cdr
functions.
The car and cdr of a cons can be set with the rplaca
(replace
car) and rplacd
(replace cdr) functions.
car list | Function |
This returns the car of list which must be either a cons
or nil
. The car
of nil
returns nil
. For
example:
car(cons(S(a), S(b))) => a car(list(L(1), L(2), L(3))) => 1
cdr list | Function |
This returns the cdr of list which must be either a cons
or nil
. The cdr
of nil
returns nil
. For
example:
cdr(cons(S(a), S(b))) => b cdr(list(L(1), L(2), L(3))) => (2 3)
caar cons | Function |
cadr cons | Function |
cdar cons | Function |
cddr cons | Function |
These functions are a convenience but also are important as commonly passed function objects. They perform successive car and or cdr operations of the cons argument cons and are defined by the following equivalents
caar(x) == car(car(x)) cadr(x) == car(cdr(x)) cdar(x) == cdr(car(x)) cddr(x) == cdr(cdr(x))
rplaca x y | Function |
The argument x must be a cons and y can be any Lpp object. This changes the car of x to y and returns the cons x after it has been modified. For example:
let c = cons(L(1), L(2)); rplaca(c, S(a)) => (a . 2) c = list(S(a), S(b), S(c)); rplaca(c, L(1)) => (1 b c)
rplacd x y | Function |
The argument x must be a cons and y can be any Lpp object. This changes the cdr of x to y and returns the cons x after it has been modified. For example:
let c = cons(L(1), L(2)); rplacd(c, S(a)) => (1 . a) rplacd(c, list(L(2), L(3))) => (1 2 3)