Cons
- In Lisp,
consis a core builtin function that links two values together by creating a memory object for the first value consisting of acar(content of address register) andcdr(content of decrement register):- the
carcontains the actual value of the list item - the
cdrcontains a pointer to aconsobject containing the second value being linked - the
consobject containing the second value likewise consists of acar(the value) and acdr(pointer to the nextcons), ornil, which terminates the singly linked list
- the
- Ordered pairs:
conscan be notated(cons 1 2)(creates objects for1and2and links them, terminating withnil)conscan alternatively be notated with a dot:(1 . 2)- this is known as a “dotted pair” or a “cons pair”
- Lists:
-
The
listfunction within Lisp usesconsinternally(list 5 10 15)…is the same as…
(cons 5 (cons 10 (cons 15 nil))) -
this also means that
conscan be used to “shift” (front-append) an element onto the front of a list
-
- Trees:
-
in truth, all singly linked lists are binary trees
-
thus, binary trees can be implemented with
consrather easily(cons (cons 1 2) (cons 3 4))((1 . 2) . (3 . 4))
-