1 / 15

ANSI Common Lisp

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

bin
Download Presentation

ANSI Common Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 functionschar<, char<=, char=, 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

  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 “<~A,~A>” (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 = GIANTSHAPE = SPHERICALNIL

More Related