User Tools

Site Tools


ch1_4_conscells

This is an old revision of the document!


List construction with conscells

There is no list

We have learned that Lisp expressions consist of atoms and lists, but in fact, there is no list object in the background. A list in Lisp is constructed with objects called conscells, or simply cons.

Conscell is an object holding two pointers, the first points to an element of the list and the second points to the next conscell. That means Lisp lists are unidirectional linked lists.

For historical reasons, the pointers are called car and cdr.

For example, list with a sublist (a (b c) d), will be stored like this:

Proper list ends with a conscell, which has NIL in cdr.

Lisp allows us to construct list directly using cons function.

>(cons 'a (cons 'b (cons 'c nil)))
(A B C)

The code above produces exactly the same list as when using LIST function, or simply quote list:

>(list 'a 'b 'c)
(A B C)
 
>'(a b c)
(A B C)

FIXME

Show more examples, cons, car, cdr, link to List Processing Functions. Show functionality of accessors and SETF shenanigans. Link to Cycles.

ch1_4_conscells.1714839780.txt.gz · Last modified: 2024/05/04 10:23 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki