SLIDE 1 ✬ ✫ ✩ ✪
A little bit
Lisp In tro du tion to Arti ial In telligen e CSCE 476-876, Spring 2012
hou ei ry/ S1 2-4 76- 87 6 Read L WH: Chapters 1, 2, 3, and 4. Ev ery re itation (Monda y): ask y
questions
Lisp/xema s. Berthe Y. Choueiry (Sh u-w e-ri) (402)472-5444 B.Y. Choueiry 1 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 2 ✬ ✫ ✩ ✪
F eatures
Lisp 1. In tera tiv e: in terpreted and
2. Sym b
3. F un tional 4. Se ond
language but still `widely' used (Ema s, AutoCad, Ma Syma, Y aho
Orbitz, et .) Soft w are/Hardw are
e ha v e Allegro Common Lisp (b y F ran In .): alisp and mlisp
are man y
and new diale ts (CormanLisp, Ky
CL, LeLisp, CMU CL, SBCL, ECL, Op enMCL, CLISP , et .)
ha v e also b een Lisp ma hines (Sym b
Conne tion Ma hine, IT Explorer,
B.Y. Choueiry 2 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 3 ✬ ✫ ✩ ✪
Lisp as a fun tional language (fun tion-name arg1 arg2 et ) 1. Ev aluate argumen ts 2. ev aluate fun tion with argumen ts 3. return the result F un tions as argumen ts to
fun tions: (name2 (name1 arg1 arg2 et ) arg3 arg2 et ) B.Y. Choueiry 3 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 4 ✬ ✫ ✩ ✪
Sym b
language
toms: n umeri atoms (n um b ers), sym b
atoms (sym b
Ea h sym b
has: prin t-name, plist, pa k age, sym b
alue, sym b
(A B C) NIL (A (B C) D) A NIL NIL D B C A B C
Sym b
expressions: sym b
and lists B.Y. Choueiry 4 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 5 ✬ ✫ ✩ ✪
More
t yp es: atoms and lists, pa k ages, strings, stru tures, v e tors, bit-v e tors, arra ys, streams, hash-tables, lasses (CLOS), et . NIL, T, n um b ers, strings: sp e ial sym b
ev aluate to self
fun tions: first ( ar), rest ( dr), se ond, tenth setf: do es not ev aluate rst argumen t
append, equal,
erations
sets, et .
ma ros: defun, defma ro, defstru t, def lass, defmethod, defvar, defparameter B.Y. Choueiry 5 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 6 ✬ ✫ ✩ ✪
e ial forms: let, let*, flet, labels, progn,
listp, endp, atom, numberp, symbolp, evenp,
et .
if <test> <then form> <else form>, when <test> <then form>, unless <test> <else form>,
ase
dolist, dotimes, do, map ar, loop,
b da fun tions B.Y. Choueiry 6 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 7
✬ ✫ ✩ ✪
A really fun tional language
(defun <function-name> <arg1> <arg2> <arg3> ... ........ <return some value>) (flet ((local-function-name <arg a> <arg b> .... #’(lambda (x) ..........) ..) (.............. <some-value>)) ) Regular function Anonymous function Local function
defun, flet/labels, lambda B.Y. Choueiry 7 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 8 ✬ ✫ ✩ ✪
What mak es Lisp dieren t? Par adigms
AI Pr
amming, Norvig
supp
for lists
storage managemen t (garbage
t yping
fun tions (dynami ally reated, anon ymous)
syn tax
tera tiv e en vironmen t
y B.Y. Choueiry 8 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 9 ✬ ✫ ✩ ✪
Allegro Common Lisp
ree do wnload: www.franz. om/downl
s/
v ailable
SunOS ( s e.unl.edu), and Lin ux.
in tegration with ema s Che k www.franz. om/ema s/ Che k
distributed b y instru tor
dev elopmen t en vironmen t Comp
debugger, insp e tor, time/spa e proler, et . (require ' omposer) B.Y. Choueiry 9 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 10 ✬ ✫ ✩ ✪
;;;
Pa kage: USER; Mode: LISP; Base: 10; Syntax: Common-Lisp
(in-pa kage "USER") ;;;; +==================== === == === === === === === === === === === === ==+ ;;;; | Sour e
for the farmer, wolf, goat, abbage problem | ;;;; | from Luger's "Artifi ial Intelligen e, 4th Ed." | ;;;; | In
to exe ute, run the fun tion CROSS-THE-RIVER | ;;;; +==================== === == === === === === === === === === === === ==+ B.Y. Choueiry 10 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 11
✬ ✫ ✩ ✪
;;; +================== === === == === === === === === === =+ ;;; | State definitions and asso iated predi ates | ;;; +================== === === == === === === === === === =+ (defun make-state (f w g ) (list f w g )) (defun farmer-side (state) (nth state)) (defun wolf-side (state) (nth 1 state)) (defun goat-side (state) (nth 2 state)) (defun abbage-side (state) (nth 3 state)) B.Y. Choueiry 11 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 12
✬ ✫ ✩ ✪
;;; +================== === =+ ;;; | Operator definitions | ;;; +================== === =+ (defun farmer-takes-self (state) (make-state (opposite (farmer-side state)) (wolf-side state) (goat-side state) ( abbage-side state))) (defun farmer-takes-wolf (state) ( ond ((equal (farmer-side state) (wolf-side state)) (safe (make-state (opposite (farmer-side state)) (opposite (wolf-side state)) (goat-side state) ( abbage-side state)))) (t nil))) B.Y. Choueiry 12 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 13
✬ ✫ ✩ ✪
(defun farmer-takes-goat (state) ( ond ((equal (farmer-side state) (goat-side state)) (safe (make-state (opposite (farmer-side state)) (wolf-side state) (opposite (goat-side state)) ( abbage-side state)))) (t nil))) (defun farmer-takes- abbag e (state) ( ond ((equal (farmer-side state) ( abbage-side state)) (safe (make-state (opposite (farmer-side state)) (wolf-side state) (goat-side state) (opposite ( abbage-side state))))) (t nil))) B.Y. Choueiry 13 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 14 ✬ ✫ ✩ ✪
;;; +================== =+ ;;; | Utility fun tions | ;;; +================== =+ (defun
(side) ( ond ((equal side 'e) 'w) ((equal side 'w) 'e))) (defun safe (state) ( ond ((and (equal (goat-side state) (wolf-side state)) (not (equal (farmer-side state) (wolf-side state)))) nil) ((and (equal (goat-side state) ( abbage-side state)) (not (equal (farmer-side state) (goat-side state)))) nil) (t state))) B.Y. Choueiry 14 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 15
✬ ✫ ✩ ✪
;;; +========+ ;;; | Sear h | ;;; +========+ (defun path (state goal &optional (been-list nil)) ( ond ((null state) nil) ((equal state goal) (reverse ( ons state been-list))) ((not (member state been-list :test #'equal)) (or (path (farmer-takes-self state) goal ( ons state been-list)) (path (farmer-takes-wolf state) goal ( ons state been-list)) (path (farmer-takes-goat state) goal ( ons state been-list)) (path (farmer-takes- abb age state) goal ( ons state been-list))) ))) B.Y. Choueiry 15 Instru tor's notes #3 Jan uary 20, 2012
SLIDE 16
✬ ✫ ✩ ✪
;;; +================== + ;;; | Canned Exe ution | ;;; +================== + (defun ross-the-river () (let ((start (make-state 'e 'e 'e 'e)) (goal (make-state 'w 'w 'w 'w))) (path start goal))) B.Y. Choueiry 16 Instru tor's notes #3 Jan uary 20, 2012