Professional Documents
Culture Documents
1. Arrays
2. Example: Binary Search
3. Strings And Characters
4. Sequences
5. Example: Parsing Dates
6. Structures
7. Examples: Binary Search Trees
8. Hashtables
Notes:
other (than lists) data structures - arrays (including vectors and strings),
structures and hashtables.
Not as flexible as lists, but can make access faster, and take up less space.
CL has one other datastructure, the instance. Covered in Chapter 11, which
describes CLOS.
1. Arrays
In CL you make an array by calling (make-array ..) with the dimensions of the
required array as the first parameter.
To set an array element, use setf with aref to specify the position
[4]> (setf (aref arr 0 0) 2)
2
[5]> arr
#2A((2 NIL NIL) (NIL NIL NIL))
[6]> (aref arr 0 0)
2
To make a one dimensional array just use a single integer instead of a list of
dimensions.
[16]> (vector 1 2 3)
#(1 2 3)
[17]> #( 1 2 3)
#(1 2 3)
Comments in lisp
;; this is a comment
[21]> #\A
#\A
[22]> (char-code #\A)
65
[23]> (code-char 65)
#\A
you can use aref or the faster char to replace characters in string.
concatenate appends many strings together, with the first parameter indicating the
type.
[38]> (concatenate 'string "hello " " there" " and bye")
"hello there and bye"
4. Sequences
In common lisp, the type sequence includes both vectors and lists (so also strings)
some of the functions we have been using are actually sequence functions, including
remove, length, subseq, reverse, sort, every, and some.
All these functions work for vectors as well.
So far we have seen four functions that work to retrieve elements of sequences.
nth for lists, aref or svref for vectors, and char for strings.
CL provides a function elt that works for any sequence.
many sequence functions take one or more keyword arguments from the standard set
listed below
:test takes a function of two arguments that it uses for equality testing. The
default is eql.
To match a list, you can use equal instead
The test function can be *any* function (_ that takes two elements and returns a
boolean).
so we could find the first position of an element of a sequence less than a given
value by
Functions similar to member, member-if for sequences are find and find-if. Unlike
the member functions they return only the object they were looking for.
find takes all keyword arguments. find-if takes all keyword arguments except :test.
In the simplest case, it will be called with the first two elements of the
sequence, and therafter with successive elements of the list as the second
argument, and the value it returned the last time as the first argument.
so
6. Structures
You can think of a structure as a vector with all these functions defined for you.
we use
(defstruct solid
l
w
h)
Section 2.3 mentioned that lisp programs can write programs. defstruct is an
example.
When you call defstruct it automatically writes code defining several other
functions.
With macros, we can do the same, and reconstruct defstruct if necessary.
to initialize a struct (_ note: this is different from scheme etc where calling the
constructor function directly is enough, here we use setf
(setf p (make-solid :l 0 :w 0 :h 0))
[28]> s
#S(SOLID :L 0 :W 0 :H 0)
[29]> (solid-l s)
0
[30]> (solid-p s)
T
[31]> (typep s 'solid)
T
We can specify default values for the structure's fields by having a list of a
field name and a default-value generating expression instead of just the field name
in the original definition.
[32]> (defstruct polemic (ptype (progn (format t "what type of polemic was it?")
(read) )) (effect "none"))
POLEMIC
[33]> (make-polemic)
what type of polemic was it?scathing
#S(POLEMIC :PTYPE SCATHING :EFFECT "none")
customizing what prefix to add for fields (default is the struct name) and printing
function elided.
8. Hashtables
To retrieve the value associated with a key we use gethash with a key and a
hashtable
(here) because the second value is nil we know that there is no value held under
this key and the first nil is 'just a default'.