Presentation is loading. Please wait.

Presentation is loading. Please wait.

ANSI Common Lisp 4. Specialized Data Structures 7 June 2003.

Similar presentations


Presentation on theme: "ANSI Common Lisp 4. Specialized Data Structures 7 June 2003."— Presentation transcript:

1 ANSI Common Lisp 4. Specialized Data Structures 7 June 2003

2 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.

3 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))

4 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.

5 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 functions char, char>=, char/=

6 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.

7 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

8 Sequences Sequence includes lists and vectors (and strings). Keyword arguments ParameterPurposeDefault :keyA function to apply to each element. identity :testThe test function for comparison.eql :from-endIf ture, work backwards.nil :startPosition at which to start.0 :endPosition at which to stop.nil

9 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.

10 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)))

11 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.

12 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))

13 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 “ ” (point-x p) (point-y p)))

14 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)

15 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 = GIANT SHAPE = SPHERICAL NIL


Download ppt "ANSI Common Lisp 4. Specialized Data Structures 7 June 2003."

Similar presentations


Ads by Google