General Functions for Both Lists and Vectors:
(quote
anything) returns anything
itself, without evaluating what it is. It may be abbreviated
by a single quote: '
This is perhaps the easiest way to construct a list or vector--just type
the list or vector and preceed it by a single quote.
;; note that LISP is case-insensitive; it converts all symbols to upper-case. >(quote (this will not be evaluated)) (THIS WILL NOT BE EVALUATED) >'(single quote works the same) (SINGLE QUOTE WORKS THE SAME) |
(setf
variable value)--assigns
its second argument to the symbol given as its first argument.
> (setf myscale '(a4 b4 c4 d4 e4)) (A4 B4 C4 D4 E4) > myscale (A4 B4 C4 D4 E4) ;; you can assign multiple values using setf by listing pairs of variable names and values: > (setf variable1 'value1 variable2 'value2 variable3 'value3) VALUE3 ;; it prints the last value assigned > variable2 VALUE2 |
(length
list-or-vector) will
return the number of elements on the top-level
of the array or vector.
> (length myscale) 5 > (length '#(1 2 3)) 3 |
(reverse
list-or-vector) will return
a vector or array whose elements are the top-level
elements of list-or-vector in reverse
order.
> (reverse myscale) (E4 D4 C4 B4 A4) > (reverse '#(1 2 3)) #(3 2 1) |
(coerce
list 'vector) will
transform list into
an equivalent vector.
(coerce vector 'list) will transform vector into an equivalent list. > (coerce myscale 'vector) #(A4 B4 C4 D4 E4) > (coerce '#(1 2 3) 'list) (1 2 3) |
List Constructors:
cons
takes exactly two arguments, the second of which
should be a proper list, and puts the two arguments together
to form a larger list. You can think of cons as tucking
the first argument inside the opening parenthesis of the second argument.
> (cons 1 '(2 3)) ;; 1 is tucked inside the opening parenthesis of (2 3) (1 2 3) > (cons '(1 2) '(3 4)) ;; (1 2) is tucked inside the opening parenthesis of (3 4) ((1 2) 3 4) |
list
takes any number of arguments and just
puts them all in order in a list.
> (list 1 '(2 3) "listing") (1 (2 3) "listing") |
append
takes two or more lists and joins them
all together, stripping away one set of parenthesis from each list.
> (append '(1 2) '(3 4)) (1 2 3 4) > (append '(1 2 (3)) '(4 5 (6))) ;; only one level of parenthesis stripped. (1 2 (3) 4 5 (6)) |
(listp
object) will return
T if object is a list
and NIL otherwise.
> x (A B C D E F G H I J) > (listp x) T > (listp '#(4 5)) NIL |
List Accessors:
car
or first refer to the first
element in a list. If that element is a single value or
symbol, it returns that symbol. If the first element of list
is itself a list, it returns that list.
> (car '(1 2)) 1 > (first '((1 2) (3 4))) (1 2) |
cdr
or rest refer to everything
after the first element of a list. The cdr of a
proper list (not a cons with a dot separating the last items) will
always be either another list or NIL.
> (cdr '(1 2)) (2) > (rest '((1 2) (3 4) (5 6))) ((3 4) (5 6)) > (cdr '(1)) NIL |
second,
third, ... , tenth--access the appropriate member of the list.
>(setf x '(a b c d e f g h i j)) (A B C D E F G H I J) > (fifth x) E |
(nth
i list)--the
nth function picks the top-level element
of list whose index is i,
starting with 0 from left to right.
> (nth 0 x) ;; using x as defined above A > (nth 4 x) ;; note that because the index starts at 0 that (nth 4 x) is equivalent to (fifth x). E |
last--returns
the last cons
of a list--in effect, the last element of the list wrapped in parenthesis.
So if you want just the last element, you need to use (car (last
list)).
> (last x) (J) > (car (last x)) J > (last x 3) ;; you can pass last an optional number of last items to return. (H I J) |
(nthcdr
i list)--goes to
the ith element of the list (again indexed from 0) and returns a list containing
that element and everything that follows.
> (nthcdr 0 x) ;; returns all of x, because 0 is the index of its first element (A B C D E F G H I J) > (nthcdr 4 x) (E F G H I J) |
(butlast
list optional-number-excluded)--(butlast
list) returns a list containing all but the last single
cons of the list. If you include an integer as optional-number-excluded,
the returned list is the same as the original list without the final optional-number-excluded
elements.
> (butlast x) (A B C D E F G H I) > (butlast x 2) (A B C D E F G H) |
(setf
(nth index list)
value) --this construction
uses (nth index
list) to point to the position in memory where value
is to be stored, allowing you to change a single value in a list.
> x (A B C D E F G H I J) > (nth 1 x) ;; setf will be pointing the this memory location. B > (setf (nth 1 x) 'beta) BETA > x (A BETA C D E F G H I J) |
vector
takes any number of arguments and just
puts them all in order in a vector.
> (vector 1 '(2 3) "listing") #(1 (2 3) "listing") |
(make-array
length-of-vector) creates an empty
vector with length-of-vector elements.
> (setf z (make-array 5)) #(NIL NIL NIL NIL NIL) |
Vector Predicate:
(vectorp
object) will return
T if object is a vector
and NIL otherwise.
> z #(NIL NIL NIL NIL NIL) > (vectorp z) T > (vectorp '(a4 b4 c4 d4 e4)) NIL |
Vector Accessor:
(aref
vector index)
returns the nth element of the top-level
of vector. Like lists,
vectors are indexed beginning with the number 0, so a vector of length
n will have indices ranging from 0 to n-1.
> y #(A B C D E F G H I J) > (aref y 4) E |
Single Value Assignment in Vectors:
(setf
(aref vector index)
value) --this construction
uses (aref vector
index) to point to the position in memory where value
is to be stored, allowing you to change a single value in a vector.
> y #(A B C D E F G H I J) > (aref y 4) ;; setf will be pointing the this memory location. E > (setf (aref y 4) 'epsilon) EPSILON > y #(A B C D EPSILON F G H I J) |