-
Notifications
You must be signed in to change notification settings - Fork 0
module std.cons
Jan Špaček edited this page Apr 12, 2016
·
1 revision
A cons pair is an immutable pair of values, traditionally called car and cdr.
Even though there is no restriction on the values stored in a cons, it is
intended to be used to make linked lists. The empty list is commonly represented
with false
.
-
Cons cells
-
(cons a d)
makes a new cons with car ofa
and cdr ofd
. -
(cons? c)
returns true ifc
is a cons cell. -
(car c)
and(cdr c)
return the corresponding component. -
(caar c)
,(cadr c)
,(cdar c)
and(cddr c)
are composedcar
s andcdr
s.
-
-
Lists
-
null
is an alias for false, representing an empty list. -
(null? x)
is an alias fornot
, more readable when used with lists. -
(list? xs)
returns true ifxs
isnull
or a cons pair where cdr is a list. -
(list-len xs)
returns the length of linked listxs
. -
(list-tail xs k)
drops the firstk
elements from the listxs
and returns the rest. -
(list-reverse xs)
reverses the listxs
.
-
-
Lists as sets and maps
-
(member-eqv x xs)
tries to find an element inxs
that iseqv?
to x. It returns the first sublist containing that value or null ifx
is not present in the list. -
(member-equal x xs)
does the same asmember-eqv
, but usesequal?
for comparison. -
(assoc-eqv key pairs)
looks upkey
inpairs
, which should be a list of cons key-value cells. It returns the matching value or null if it is not found. -
(assoc-equal key pairs)
works asassoc-eqv
, but compares withequal?
.
-