Docsity

Lisp Notes: Understanding Artificial Intelligence Concepts through Lisp Programming - Prof, Study notes of Computer Science

Partial preview of the text

Download Lisp Notes: Understanding Artificial Intelligence Concepts through Lisp Programming - Prof and more Study notes Computer Science in PDF only on Docsity!

CAP 4621 and 5635

Artificial

Intelligence

Concepts

Lisp Notes

© 1999, Douglas D. Dankel

LISP (LISt Processing)

  • Is both:
    • Programming language, 1958, John McCarthy,MIT
    • Format mathematical language, A. Church,Lambda Calculus
  • Designed for list processing & manipulation of symbolic information
  • No Distinction between program and data
    • great flexibility and power
    • integration of declarative and proceduralknowledge

LISP (LISt Processing) (cont)

  • Called the assembler language of A.I.
  • Highly Recursive
  • Uses prefix notation
  • Works on objects or S-expressions (symbolic expressions): – word-like object called an atom - group or list of S-expressions
  • Top level consists of: (1) Read (2) Evaluate(3) Print

Lisp Functions

A

t nil 29 ‘(29 t at) (setq x 4)

x (set ‘x ‘b) x (set x 5) x b (setq c (list x 5 t) )

(car ‘(a b c) ) (car ‘( (a b) c d) ) (cdr ‘(a b c) ) (cdr ‘( (a) (b c) d) ) (cons ‘x ‘(b c) ) (cons ‘(x) ‘(b c) ) (append ‘(c d) ‘(a b) )

(append ‘( (c) ) ‘( (d) ) ) (cadr ‘(a b c) ) (caddar ‘( (a b) (d e) ) ) (cdr ‘(c) ) (cddr ‘(c) ) (car nil)

(min 7 12 -3 9) (mod 12 5) (random 10) (setq a (complex 7 9) ) (imagpart a) (realpart a)

Testing Functions

(atom ‘a) (atom ‘(a b) ) (atom 5) (numberp 5) (floatp 5)

(listp 5) (constantp 5) (constanp ‘a) (symbolp ‘a) (symbolp 10) (typep 10 ‘integer) (type-of “hello”)

(equal ‘a ‘a) (equal ‘(a) ‘a) (not nil) (not t) (not ‘a) (null ( ) ) (null 7)

Lisp Internals

(setq a ‘(a b c) )

(setq b a)

(eq a b)

(equal a b) (setq c ‘(a b c) )

(eq a c) (equal a c) (eval (car a) )

(setq b (cons ‘c (cdr b) ) )

(rplaca a ‘(1 2) )

(rplacd (cdr b) ‘(c d) )

(setf (cadr a) ‘g)

Additional List Functions

(copy-list ‘( (a) (b) c) )

(list-length ‘(1 3 (4 5) 6 (7 8 9) 10) )

(remove ‘6 ‘(5 6 7 6 (1 6 3) 6) )

(remove-duplicates ‘(5 5 6 7 6 3 9 5) )

(reverse ‘(1 2 (3 4) 5 6) )

(subst 5 6 ‘(6 7 8 (6 5) 4 6) ) (remove ‘(b c) ‘(a (b c) (d e) (b c) ) ) (remove ‘(b c) ‘(a (b c) (d e) (b c) ) :test #’equal)

Functions

(defun fname ( parm * [ &optional { parm | ( parm val ) } + ] [ &rest parm ] ) { decl | doc-string } * form * )

(defun dist-from-origin (x y &optional (z 0) ) “computes 3-d distance from origin” (sqrt (+ (* x x) (* y y) (* z z) ) ) )

(dist-from-origin 3 4) (dist-from-origin 3 4 5) (pprint (symbol-function ‘dist-from-origin)) (documentation ‘dist-from-origin ‘function) (defun num-args (&rest alist) (list-length alist) ) (num-args ‘a ‘b ‘(c d) 15 ‘e)

Scope

(defun free-function (x) (setq y (* z x) ) (setq z (+ z y) ) )

(setq x ‘(a b c) y ‘(d e) z ‘(f g h) )

(free-function 5)

x

y

z

File I/O

(load file ) (open file [ :direction ] ) (setq in-file (open “data.lsp” :direction :input) ) (close stream ) (close in-file)

:input:output :io

(rename-file old new )

(delete-file file )

(probe-file file )

Applicative & Map Functions

(apply ‘append ‘( (1 2 3) ( 4 5 ) ( 6 7 8 ) ) )

(funcall ‘append ‘( 1 2 3 ) ‘( 4 5 ) ‘( 6 7 8 ) )

(mapcar ‘print ‘( a b c ) )

(mapcar ‘1+ ‘( 1 2 3 ) )

(mapcar ‘+ ‘( 1 2 3 ) ‘( 4 5 6 ) ‘( 7 8 ) )

(mapcar ‘print ‘( 1 2 3 ) )

Structured Programming

LET

(let ( [ var | ( var value ) ] * ) [ decl ] * [ forms ] * ) LET*

  • same structure
  • var s assigned values sequentially not in parallel (defun local-fun ( x ) (let ( (y 10 ) z ) (setq y (* z y) ) (setq z (+ x y) ) ) )

(defun local-fun2 ( x ) (let* ( (y ‘(a b c) (z (cdr y) ) ) (setq y (cons x y) ) (setq z (cons x z) ) ) ) LOOP (loop [ form ] * ) (defun test ( lst ) ( loop (print (car lst) ) (setq lst (cdr lst) ) (when (null lst) (return t) ) ) )