Dana Vrajitoru
C311 Programming Languages
Lisp Arrays, Equality Predicates, Symbols, Associative
Lists
Arrays in Lisp
- An array is a sequence of contiguous memory locations in which we
can store objects of any kind.
- There are four types of arrays in Elisp: strings, vectors,
bool-vectors, and char-tables.
- They are indexed starting from 0. In lisp they are 1-dimensional.
- Both lists and arrays are sequences.
Array Functions
- Constant arrays:
(setq a '[1 2 3 45 65]) ; [1 2 3 45 65]
- Using the functions vector and make-vector:
(setq b (vector 'a 32 nil '(1 2 3)))
; [a 32 nil (1 2 3)]
(setq sleepy (make-vector 5 'Z));[Z Z Z Z Z]
- Concatenating vectors:
(vconcat [A B C] "aa" '(foo (6 7)))
; [A B C 97 97 foo (6 7)]
- Accessing / modifying an element of an array:
(elt a 3) ; 45
(aset b 2 'monday) ; [a 32 monday (1 2 3)]
Symbols
- A symbol has 4 attributes (cells):
- print name - to be called by symbol-name, its name
for printing
- value cell - contains its value as a
variable, symbol-value
- function cell - the function
definition, symbol-function
- property list cell - its meaning as a property list,
symbol-plist.
- Symbols can exist in the program without any of these cells being
defined. 'a is interpreted as the symbol a even if it's not defined as
a function or variable. Symbols are unique.
Examples of Symbols
(make-symbol "wolf") ;=> wolf
(symbol-name 'wolf) ;=> "wolf"
(setq dog (make-symbol "dog")) ;=> dog
(symbol-name dog) ;=> "dog"
(symbol-name 'dog) ;=> "dog"
(setq goat "fluffy") ;=> "fluffy"
goat ;=> "fluffy"
(symbol-name 'goat) ;=> "goat"
(symbol-value 'goat) ;=>"fluffy"
Equality Predicates
- There are 3 predicates that test the equality of objects in Elisp.
- = - tests if two numbers are equal. Returns an error if one
of the arguments is not a number.
- eq - tests if the two arguments are identical objects in
the memory.
- equal - tests if the two arguments have similar content. If
the objects are containers (lists, arrays), it compares the size and
the corresponding elements.
Examples
(setq L '(1 2 3 4)) ;=> (1 2 3 4)
(setq M '(1 2 3 4)) ;=> (1 2 3 4)
(eq L M) ;=> nil
(equal L M) ;=> t
(setq M L) ;=> (1 2 3 4)
(eq L M) ;=> t
(eq '(1 2 3) '(1 2 3)) ;=> nil
(eq "hello world" "hello world") ;=> nil
(eq 1 1) ;=> t
(eq 1 1) ;=> t
(eq 'L 'L) ;=> t
(setq M 'L) ;=> L
(eq 'L M) ;=> t
Associative Lists
- A list where the elements are pairs of keys associated with values.
(setq flowers '((rose . red) (lily . white) (iris . purple)))
- It makes it easy to find values associated with the keys:
(assoc 'lily flowers ) ;=> (lily . white)
(rassoc 'purple flowers ) ;=> (iris . purple)
- assq, rassq - same as assoc and rassoc but use eq instead of equal.
- copy-alist - a 2-level deep copy of the list.
Example
(setq A '((jan . 31) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30)))
;((jan . 31) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30))
(assoc 'feb A) ; (feb . 28)
(rassoc 29 A) ; (feb . 29)
(rassoc 31 A) ; (jan . 31)
(assq 'mar A) ; (mar . 31)
(setq M 'mar) ; mar
(assq M A) ; (mar . 31)
Some Functions
(defun copyalist (L)
"Copies an associative list."
(let ((newlist ()))
(dolist (e L newlist)
(setq newlist
(append newlist
(list (cons (car e)
(cdr e)
)))))))
(defun setassoc (sym val L)
"Associates the value with the symbol in the list L."
(setcdr (assoc sym L) val))
(setq B (copyalist A))
; ((jan . 31) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30))
(setcdr (assoc 'jan B) 32) ; 32
B ; ((jan . 32) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30))
A ; ((jan . 31) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30))
(setassoc 'apr 30 B) ; 30
B ; ((jan . 32) (feb . 28) (mar . 31) (feb . 29) (apr . 30) (may . 30))
A ; ((jan . 31) (feb . 28) (mar . 31) (feb . 29) (apr . 31) (may . 30))