LISP Notes For Artificial Intelligence (CIS 479)

LISP

‘(a b c)

a is an atom

b is an atom

c is an atom

CLOS- Common Lisp Object System

Atoms- Numeric 12

-1.53

Literal a

} These always need to be defined

Sam

T or t True

} These values are predefined in the language

Nil

Nil = ‘ ( ) "empty list"

‘(a b c)

car cdr

(first) rest

LISP Prompt

>12

12

>a

undefined value

>(exit)

Takes you out of xlisp.

‘(a b c)

(set ‘x 3)

>x

3

(setq x 3)

>x

3

(set x 3)

(setq x ‘(a b c))

(a b c)

>(+1 3) * This will add 1 and 3

4

>(+1 3 4 5 7) * This will add1 to 3 to 4 to 5 to 7

20

>(- 1 3) * This will subtract

2

>(* 1 3) * This will multiply

3

>(/ 1 3) * This will divide

3

>(setq x 3)

x=3

(setq x (1+ x))

2 * 2 + 4 / 5

(+ ( * 2 2) ( / 4 5 ))

>(car ‘(a b c))

a

>(car ‘((a b) c d e))

(a b)

>(car (cdr ‘(a b c)))

(b c)

b

>(cdr (car ‘(a b c))

run – time error (no list)

>(cadr ‘(a b c))

b

up to 4

(cxxxxr )

>(list ‘a ‘b)

(a b)

>(list ‘a ‘b ‘(c d))

(a b (c d))

{LISP Documentation is a little less than 150 pages from the web page for xlisp}

>(cons ‘a ‘(b c))

(a b c)

Dotted Pair

(cons ‘a ‘b)

>(append ‘(a b)(c d))

(a b c d)

(cons ‘(a b) ‘(c d))

((a b) c d)

(reverse (a b ( c d))

(length )

(last )

>(subst ‘a ‘b ‘(a b c)) * This will replace b (second item) with an a (first item)

‘(a a c)

>(eval (cdr ‘ (a + 2 3)))

(eval ‘(+ 2 3))

5

>(+ 2 3)

5

>(setq ‘a ‘b)

b

>(setq b 3)

3

>a

b

>(eval a)

3

>(eval (eval ‘a))

3

>(eval ‘a)

a

(defun intro (x y)


(list x 'this 'is y)
)

(intro 1 2)

(1 this is 2)

Predicate Functions

( atom )

( listp )

( equal )

( = )

( assoc )

( member )

(defun comp ( op x y)
  (cond (( equal op ‘sum) (+ x y))
        (( equal op ‘prod) ( * x y))
        (( t ‘(does not compute))
  )
)

(if cond then else)

(if (listp a) (car a)) ‘(not a list))

else

(case b (‘a ‘1st) (‘b ‘2nd) (‘c ‘3rd))) à 2nd

(let (( item first)

(( par1 val1)

( par2 val2)

( par3 val3)

)

)

(defun aug (first second)
  (let (( item first)
         ( bag second)
       )
    (cond ((listp first ) (setq item second) (setq bag first))
    )
    (cond ((member item bag) bag)
          ( T (cons item bag))
    )
  )
)

T or nil

(atom ‘digits) à T

(atom ‘(a b c)) à nil

(listp ‘(a b c)) à T

(equal x y) à T (same data type and value)

(eq 4 (/ 8 2)) à nil (checks for identical objects)

(= , <, >, <=, >=)

(null ‘( )) à T

(null nil) à T

(null T) à nil

(member ‘a ‘(a b c)) à (a b c)

(member ‘a ‘((a b) c (d e))) à nil

(member ‘(d e) ‘((a b) c (d e) f )) à ((d e) f)

(assoc ‘a ‘((a b) (c d) (e f))) à (a b)

(and nil T T ) à nil

(and ‘george nil ‘harry) à nil

(and 1 2 3 4 5 ) à 5

(defun sign (a b)
   (and (odd p a) (odd p b) ‘both-odd))
)

(sig 3 5) à (both-odd)

OR * this returns the first non nil value

(or nil T T) à T

(defun same-sign (x y)
  (or (and (zerop x) (zerop y))
      (and (< x 0) (< y 0))
      (and (> x 0) (> y 0))
  )
)

mn = m * mn-1 for n > 0

mn = 1 for n = 0

(defun expon (m n)
  (if (zerop n) 1 (* m (expon m (1- n))))
)

(defun count-atom (L)
  (cond ((null L) 0)		; empty list
        ((atom L)		; atom
        ( t (+ (count-atom (car L)) (count-atom (cdr L))))
   )
)

(apply `+ `( 2 3 )) à 5

(eval `( + 2 3 )) à 5

(apply `cons `(as (you like it)))

Iterator

(mapcar `addp ‘(1 2 3)) à ( t nil t)

(mapcar `square ‘(1 2 3 4 5 )) à ( 1 4 9 16 25 )

(setq words ‘((one eins)) (two zwei) (three drei)))

(mapcar `car words) à ( one two three)

(mapcar `cdr words) à ( ein zwei drei )

(mapcar `reverse words) à (( eins one) (zwei two) ( drei three))

(defun translate (x)
  (cadr (assoc x words))
)

(mapcar `translate ‘(three one two one)) à (drei eins zwei eins)

Anonymous Functions

(mapcar #`(lambda (x) (* x x)) `(1 2 3 4 5)) à ( 1 4 9 16 25 )

(mapcar `reverse words)

((eins one) (wei two) (drei three))

(mapcan `reverse words)

(eins one zwei two drei three)

(maplist `reverse words)

(((three drei) two zwei) (one eins))

((three drei) ( two zwei))

(one eins))

(mapcon `reverse words)

((three drei) (two zwei) (one eins)

(three drei) (two zwei)

(three drei)

Prog and Loops

(defun exp2 m n)
  (prog ((result 1)
         (expon n)
        )
        loop1
          (cond ((zerop expon) (return result)))
          (setq result (m result))
          (setq expon (1- expon))
          (go loop1)
   )
)

(progn (setq x 'foo) (setq x 'bar) (setq x 'bar) (list 'x 'is x) 'done<)

(defun count (L)

(do ((cnt 0 ( 1 + cnt)) (loaf L (cdr loaf)) ) ((null loaf) cnt) ) )

count `(a b c)) à 3

(defun fact (n)
  (do ((I 1 ( I + 1))
       (res 1)
      )
      ((> I n ) res )
      (setq res (* res I))
  )
)

(dolist (x `(a b c) y) (setq y (list x)))

(dotimes (x 3 x)) ; first x is the loop variable

; the three is the number of times it will loop

0 ; this is the return value

1

2

Property List

(putprop ‘fred ‘male ‘sex) ; fred is the object

; male is the value

; sex is the attribute

(symbol-plist ‘fred) à (sex male)

(get ‘fred ‘sex) à male

(remprop fred ‘sex) à nil

(setf (get ‘fred ‘sex) ‘female)

(setf (symbol-plist) `(sex male age 23 sits (george wanda sally))

Strings

(char "sam eats soup" 5) = 65

(string 97) à "a"

(explode `a b c) à ("a" "b" "c")

(strcat "a""b""c") à "a b c"

subseq "sam eats soup" 5 3) à ats

(make-string 5 "a") à "aaaaa"

(substring " a b c d e" 2 5) ; two is the begin point and 5 is the ending point

(string-reverse "a b c d") à "b c b a"

Input / Output

(read)

(setq a (read))

(defun batch (fn1 fn2)
   (do* ((old (open fn1: direction: input))
         (new (open fn2: direction: output))
         (ex (read ad) read old))
   )
)

(batch "a:\old.lsp" "a:\out.dat")

(prin1 "sam") à "sam" "sam"

(princ "sam") à sam "sam"

(defun out ( )

(print "sam")

(prin1 "sam")

(princ "sam")

)

(out) à "sam"

"sam" "sam" "sam"

(do* ((fp (open "plot.lsp" :direction: input)
      (ex (read fp) (read fp))
     )
     ; open and write to a file
     ((null ex) nil)
     (print ex)
)

To copy

(defun copy (fn1 fn2)
  (do* ((old (open fn1 :direction: input))
        (new (open fn2 :direction: output))
        (ex (read old) (read old))
       )
       (null ex) (close new) (close old))
       (print ex new)
  )
)

LISP Surgery

(setq abc ‘(abc))

(setq xyz ‘(xyz))

(setq bc ‘(cdr abc))

(setq xz ‘(cdr xyz))

(setq a (nconc abc xyz))

;nconc will return everything

>a

(a b c x y z)

>abc

(abc xyz)

>xyz

(xyz)

>bc

(bc xyz)

>yz

(yz)

(setq a ‘(a b c d e))

(rpla (a a ‘(ab))

a ((a b) b c d e)

(rpla cd a ‘(xyz)

((ab) xyz)

(setq a ‘( a b c d e f))

(setq a (delete a a))

(b c d e f)

Array Faciltity

(setq a (make-array 3))

a (nil nil nil)

0 1 2

Put something into the array?

(setf (aref a 2) 4)

a (nil nil 4)

look a1

(aref a1)

nil

Macro Facility

(setq a 3)

`(if a is true ( and a a) then

add comma

(if, a is true, (and a a) then

(if 3 is true 3 then

(list ‘if a ‘is ‘true (and aa) ‘is ‘true)

(setq b ‘(a b c))

`(hello fred, b)

(hello fred (a b))

`(hello fred, a b)

(hello fred a b c)

)

(defun name (x)
  (car x)
)

(defmacro name (x)
  (list ‘car x)
)

(defmacro name (x)
  `(car ,x)
)

(defmacro if2 (a b &optional c)
 `(cond (,a ,b)			; This will make c optional
        (t ,c))
)

(if2 test exp exp)

(if2 (atomp w) `yes)