Title: Data Structures in Lisp
1Data Structures in Lisp
0. Collections of associations, association
lists 1. Creating graphs with conses
RPLACA and RPLACD (destructive
editing) 2. Adjacency lists 3. Sequences 4.
Arrays 5. Hash tables 6. Strings
2Collections of Associations
A collection of associations can be thought of as
a set, a function, or a many-many mapping. CONS
is the fundamental mechanism for association in
Lisp. An association list is a list of
CONSes. However, for reasons of efficiency,
there are alternatives to represent collections
of associations in Lisp. (e.g., Hash tables)
3Direct Manipulation of Conses
gt (setq x (cons 'a 'b)) (A . B) gt (rplaca x
'c) (C . B) gt x (C . B) gt (rplacd x 'd) (C . D) gt
(rplacd x x) (C C C C C C C C C C ... ) gt
4Adjacency Lists
Nowadays, RPLACA and RPLACD are less
used. Straight lists are more common. Memory
space is less of a concern. (setq nodes '(a b c
d)) (setq arc-lists '( (a (b c)) (b (d))
(c (d)) (d ()) ) ) (defun is-edge (x y)
(member y (second (assoc x arc-lists)))
) (is-edge 'a 'c) gt (C) i.e., true. (is-edge
'a 'd) gt NIL
A
C
B
D
5Sequences
A sequence can be thought of as a mapping
from 0, 1, ..., n-1 to a set of range
elements. A string is a sequence of
characters. A list is a particular form of
sequence of lisp objects. (typep "abc"
'sequence) gt T. (typep '(a b c) 'sequence)
gt T. (concatenate 'string "abc" "def") gt
"abcdef" (concatenate 'list '(a b) '(c d)) gt(a
b c d) (elt '(a b c d) 2) gt C
6Arrays
An array is a mapping from a set of index tuples
to a set of range elements. (setq a (make-array
'(5 10)initial-element 1)) 2A((1 1 1 1 1 1 1 1
1 1) (1 1 1 1 1 1 1 1 1 1) (1 1 1 1 1 1 1
1 1 1) (1 1 1 1 1 1 1 1 1 1) (1 1 1 1 1 1
1 1 1 1)) (setf (aref a 4 9) 2) 2 (let ((sum
0)) (dotimes (i 5 sum)(dotimes (j 10)
(incf sum (aref a i j)) )) ) 51
7Hashtables
Association lists are linear and slow. For larger
sets of associations, hashtables tend to be much
faster. gt (let ((h (make-hash-table))) (defun
get-h (key) (gethash key h)) (defun put-h (key
value) (setf (gethash key h) value))
) PUT-H gt (put-h 'x 'y) Y gt (get-h 'x) Y The
value associated with X T True, there is a
value. gt
8MAPHASH
Changes each entry in THE-HASH-TABLE,
replacing each positive value with its square
root, and removing negative values. (maphash
Steele84, p285. '(lambda (key
value) (if (minusp value) (remhash
key the-hash-table) (setf (gethash key
the-hash-table) (sqrt value))))
the-hash-table) returns NIL
9Strings
A Lisp string is written as a sequence of
characters between double quotes. "This is a
string." Double quotes can be entered using the
backslash character "Here is a \"string within a
string\"." Backslashes can be included by using a
double backslash "c\\windows\\system\\temp.txt"
A string is a one-dimensional array of
characters.
10 Constructing Strings
Concatenation gt (concatenate 'string "Happy" " "
"Birthday!") "Happy Birthday!" Character-by-chara
cter manipulation gt (setq str (make-string 5
initial-element \A)) "AAAAA" gt (setf (aref str
2 \C)) \C gt str "AACAA"
11Comparing Strings
gt (string "happy" "happy") T gt (string "hapPy"
"happy") NIL gt (string/ "hapPy" "happy") 3 gt
(string-equal "hapPy" "happy") T gt (string-lessp
"John" "johnny") 4 gt (stringlt "John"
"johnny") 0 gt (equal "hapPy" "happy") NIL gt
(equalp "hapPy" "happy") T
12String Input and Output
gt (setq line (read-line))This is a line of
text. "This is a line of text." gt (print
line) "This is a line of text." "This is a line
of text." gt (format t "The input was S"
line) The input was "This is a line of
text." NIL gt (format t "The input was A"
line) The input was This is a line of
text. NIL gt (setq line2 (format nil (format t
"The input was A" line))) "The input was This
is a line of text."
13String Matching
gt (search "line" "This is a line of text.") 10 gt
(search "line" "THIS IS A LINE OF TEXT.") NIL gt
(search "line" "THIS IS A LINE OF TEXT."
test 'char-equal) 10 gt (search "xy9"
"abcde01234" key 'digit-char-p) 3 SEARCH
works not only on strings but all sequences. gt
(search '(0 1) '(0 0 0 1 1 1)) 2 gt (search '(0 1)
'(2 4 8 7 5 3) key 'evenp) 2
14Substring Extraction
gt (subseq "This is a line of text." 10
14) "line" gt (subseq "The remainder of the
line." 4) "remainder of the line." gt (setf
(subseq "Lets censor it." 6 12)
"XYZXYZXYZW") "Lets XYZXYZ it."