Dana Vrajitoru
C311 Programming Languages

Lisp Arrays, Equality Predicates, Symbols, Associative Lists, catch-throw

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

catch-throw

Syntax

(catch tag
  block of expressions) 
Meanwhile inside the block of expressions:
(throw tag return-value)

Example 1

; returns 0 if n is 0, 1 if n is positive, -1 if n is negative
(defun sign (n)
  (catch 'negative
    (catch 'zero
      (cond
       ((= n 0) (throw 'zero 0))
       ((< n 0) (throw 'negative -1))
       (t 1)))))

(sign 0) ; 0
(sign 3) ; 1
(sign -5); -1

Example 2

; searches for the value val in the list L. returns t if the value is present,
; nil if the value is not in the list.
(defun search (L val)
  (if (not L) nil ; an empty list does not contain the value
    (catch 'found ; prepare a place to return to when we find val
      (dolist (elem L nil)   ; if the loop terminates without a throw
        (if (equal elem val) ; then the value is not in it
            (throw 'found t)))))) ; exits the loop

(search	'(1 2 3 4 5) 3) ; t
(search	'(1 2 3 4 5) 8) ; nil

Example 3

; returns t if the list L is sorted, nil if it isn't.
(defun is-sorted (L)
  (if (not (cdr	L)) t ; if the list has 0 or 1 elements, it is sorted
    (let ((prev (car L))) 
      (catch 'notsorted ; return point from the throw
        (dolist (elem (cdr L) t)
          (if (> prev elem)
              (throw 'notsorted	nil)
            (setq prev elem)))))))

(is-sorted '(1 2 3 4 5 6)) ; t
(is-sorted '(2 3 4 1 2 4)) ; nil