You are on page 1of 4

Chapter 3: Lists

1. Conses
2. Equality
3. Why Lisp Has No Pointers
4. Building Lists
5. Example: Compression
6. Access
7. Mapping Functions
8. Trees
9. Understanding Recursion
10. Sets
11. Sequences
12. Stacks
13. Dotted Lists
14. Assoc Lists
15. Example: Shortest Path
16. Garbage

Notes
lists are one of the fundamental datastructure of lisp.
(but) CL is a general purpose programming language with many built in data
structures.

Often in the initial phases of programming development, a lot of lists are used.
In later versions you can switch to faster specialized data structures.

This chapter shows many things you can do with lists, and uses them to illustrate
some concepts.

1. Conses
What cons really does is combine two objects into a structure called a cons. Conses
provide a convenient representation of pairs of any type.

Conceptually a cons is a pair of pointers. One is (to) the car and the other is to
the cdr. The two halves of the cons could point to any type of object, including
conses (the latter is used to build lists)

Any non empty list can be considered as a pair of the car-value and the rest of the
list (which is either the car of a sublist or nil)

(some 'cons pictures' of flat and nested lists etc)

consp is the predicate for conses


listp for lists

[1]> (consp (cons 1 (cons 2 nil)))


T
[2]> (listp (cons 1 (cons 2 nil)))
T
[3]> (listp (cons 1 2))

both predicates seem to work for all forms of conses?

nil is both a list and an atom.

2. Equality
Each time you call cons, lisp allocates memory with room for two pointers.
If we call cons twice with the same arguments we get two values that look the same,
but not identical.

(eql (cons 'a nil) (cons 'a nil))


NIL

eql returns true only if the comparands are the same object

[4]> (eql (cons 'a nil) (cons 'a nil))


NIL
[5]> (setf l '( 1 2 3))
(1 2 3)
[6]> (eql l l)
T
[7]> (setf m '( 1 2 3))
(1 2 3)
[8]> (eql l m)
NIL

equal returns true if its arguments would 'print the same'.

[11]> (equal l m)
T
This works for objects other than lis.

;; a version of equal just for lists.


(defun our-equal (x y)
(or
(eql x y)
(and
(consp x)
(consp y)
(our-equal (car x) (car y))
(our-equal (cdr x) (cdr y)))))

3. Why Lisp Has No Pointers


lists have elements. variables have values.
conses have pointers to their elements. variables have pointers to their values.

this makes both x and y point to the same structure (area of memory)

[12]> (setf x '( 1 2 3))


(1 2 3)
[13]> (setf y x)
(1 2 3)
[14]> (eql x y)
T

Every value in lisp is conceptually a pointer, which is stored in data structures,


passed around etc (_ but these pointers are not available to the programmer)

For efficiency, sometimes the lisp implementation may store a value directly, and
not a pointer. E.g small integers don't take any more space than a pointer. but in
general pointers all the way so a structure can refer to itself.

4. Building Lists
copy list takes a list and returns a copy of it with identical values but in
different conses.

(picture).
(setf y (copy-list l))
(1 2 3)
[16]> y
(1 2 3)

(defun our-copy-list (lst)


(if (atom lst)
lst
(cons (car lst) (our-copy (cdr lst)))))

a list and its copy-list ed clone will be equal but never eql unless the original
list is nil.

append returns the concatenation of any number of lists

5. Example: Compression

run length encoding.

example:
(compress '( 1 1 1 0 1 0 0 0 0 1))
(( 3 1) 0 1 (4 0) 1)

whenever the element occurs several times in a row, those elements are replaced
with a pair the first element of which is the count and the secnod is the element.

; compress and uncompress are in chp3.lisp


; loaded with "(load "chp3.lisp") from the directory where chp3.lisp is situated.

we don't need to write (list-of ...) (make-list...) works but it has keyword
arguments which we haven't learned yet.

6. Access

Common Lisp has access functions defined in terms of car and cdr.
(nth 0 '( a b c))
A

[15]> (nthcdr 2 '( a b c))


(C)

both nth and nthcdr are zero based. Whenever you use a number to refer to the
contents of a data structure, it is zero based.

the function zerop returns zero if it argument is zero

[16]> (zerop 2)
NIL
[17]> (zerop 0)
T

the function last returns the last cons in a list

[18]> (last '( 1 2 3 ))


(3)
[19]> (last nil)
NIL

7. Mapping Functions
8. Trees
9. Understanding Recursion
10. Sets
11. Sequences
12. Stacks
13. Dotted Lists
14. Assoc Lists
15. Example: Shortest Path
16. Garbage

You might also like