100 likes | 186 Views
Data 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. Hashtables. Collections of Associations.
E N D
Data 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. Hashtables CSE 341, S. Tanimoto Lisp Data Structures -
Collections 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 for representing collections of associations in Lisp. CSE 341, S. Tanimoto Lisp Data Structures -
Association Lists Association lists store (functional) binary relations. Lisp functions ACONS and ASSOC help. > (setq fruit-colors nil) NIL > (setq fruit-colors (acons 'apple 'red fruit-colors)) ((APPLE . RED)) > (setq fruit-colors (acons 'pear ’green fruit-colors)) ((PEAR . GREEN)(APPLE . RED)) > (assoc 'apple fruit-colors) (APPLE . RED) > (assoc 'banana fruit-colors) NIL CSE 341, S. Tanimoto Lisp Data Structures -
Direct Manipulation of Conses > (setq x (cons 'a 'b)) (A . B) > (rplaca x 'c) (C . B) > x (C . B) > (rplacd x 'd) (C . D) > (rplacd x x) (C C C C C C C C C C ... ) > CSE 341, S. Tanimoto Lisp Data Structures -
Adjacency 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) ; => (C) i.e., true. (is-edge 'a 'd) ; => NIL A C B D CSE 341, S. Tanimoto Lisp Data Structures -
Sequences 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) ; => T. (typep '(a b c) 'sequence) ; => T. (concatenate 'string "abc""def"); => "abcdef" (concatenate 'list '(a b) '(c d)); =>(a b c d) (elt '(a b c d) 2) ;=> C CSE 341, S. Tanimoto Lisp Data Structures -
Arrays An array is a mapping from a set of index tuples to a set of range elements. (setq ages (make-array 4 :initial-contents ’(21 19 20 21) ) ) #(21 19 20 21) (aref ages 2) 20 (setf (aref ages 2) 25) 25 ages #(21 19 25 21) CSE 341, S. Tanimoto Lisp Data Structures -
Multidimensional Arrays (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 CSE 341, S. Tanimoto Lisp Data Structures -
Hashtables Association lists are linear and slow. For larger sets of associations, hashtables tend to be much faster. > (let ((h (make-hash-table))) (defun get-h (key) (gethash key h)) (defun put-h (key value) (setf (gethash key h) value)) ) PUT-H > (put-h 'x 'y) Y > (get-h 'x) Y ; The value associated with X T ; True, there is a value. > CSE 341, S. Tanimoto Lisp Data Structures -
MAPHASH ; Changes each entry in THE-HASH-TABLE, ; replacing each positive value with its ; square root, and removing negative values. (maphash ; Steele’84, 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 CSE 341, S. Tanimoto Lisp Data Structures -