150 likes | 252 Views
ANSI Common Lisp. 4. Specialized Data Structures 7 June 2003. Array.
E N D
ANSI Common Lisp 4. Specialized Data Structures 7 June 2003
Array • Make an array>(setf arr (make-array '(2 3) :initial-element nil))Create an 2X3 array with initial values NIL.>(setf arr (make-array '(2 3) :initial-contents '((1 2 3) (4 5 6))))Create an 2X3 array initialized with a sequence. • By default, the elements can be of any type and the value of each element is undefined.
Array • Retrieve array element>(aref arr 0 0)1 • Set array element>(setf (aref arr 0 0) 100)#2A((100 2 3) (4 5 6)) • Display an array*print-array* is t #2A((100 2 3) (4 5 6))
1-Dimensional Array (Vector) • Make 1-D array>(setf vec (make-array 4 :initial-element nil))> (setf vec (vector 1 2 3 4)) • Retrieve array elements> (aref vec 0)> (svref vec 0) • Vector is a type of CLISP type sequence, so sequence functions can be applied.
Characters • A character c is denoted as #\c. • Each character has an associated integer (generally, it’s ASCII number).char-code: returns the number associated with a character.code-char: returns the character associated with an integer. • Comparison functionschar<, char<=, char=, char>, char>=, char/=
String • String are vectors of characters. A constant string is surrounded by double-quotes. • Since strings are vectors and a vector is an 1-dimensional array, so array functions work on strings. • Since strings are vectors and vectors are sequences, so sequence functions work on strings too.
String Functions • Building strings> (setf s (format nil "~A or ~A" "live free" "die"))"live free or die“ • Join strings>(concatenate 'string "Go " "Will")"Go Will" • String comparison functions
Sequences • Sequence includes lists and vectors (and strings). • Keyword arguments
Sequences • The remove-duplicates function> (remove-duplicates "abracadacra")"bdcra"Preserves the last occurrence of an element. • The reduce function(reduce #’fn ‘(a b c d)) (fn (fn (fn ‘a ‘b) ‘c) ‘d)Extend functions that only take two arguments.
Example: Parsing Dates (defun tokens (str test start) (let ((p1 (position-if test str :start start))) (if p1 (let ((p2 (position-if #'(lambda (c) (not (funcall test c))) str :start p1))) (cons (subseq str p1 p2) (if p2 (tokens str test p2) nil))) nil)))
Example: Parsing Dates (defun constituent (c) (and (graphic-char-p c) (not (char= c #\ )))) The graphic characters are all characters we can see, plus the white space character.
Structures • A simple definition(defstruct point x y) • The make-point, point-p, copy-point, point-x and point-y functions are automatically generated. • Create a new point(setf p (make-point :x 0 :y 0))
Structures • Default values for structure fields(defstruct point (x 0) (y 0)) • More controls(defstruct (point (:print-function print-point)) (x 0) (y 0))(defun print-point (p stream depth) (format stream “<~A,~A>” (point-x p) (point-y p)))
Hash Table • Create a hash table(setf ht (make-hash-table)) • Retrieve the value given a key(gethash ‘color ht) • Associate a value with a key(setf (gethash ‘color ht) ‘red) • Remove an entry(remhash ‘color ht)
Hash Table • The maphash Function> (setf (gethash 'shape ht) 'spherical (gethash 'size ht) 'giant)GIANT> (maphash #'(lambda (k v) (format t "~A = ~A~%" k v)) ht)SIZE = GIANTSHAPE = SPHERICALNIL