Dana Vrajitoru
C311 Programming Languages

Lisp Arrays, Equality Predicates, Symbols, Associative Lists

Arrays in Lisp

Array Functions

Symbols

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

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

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