A little bit of Lisp B.Y. ✫ ✬ Choueiry In tro du tion to Arti� ial In telligen e CSCE 476-876, Spring 2012 www. se.unl.edu/~ 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 our questions on Lisp/xema s. 1 Berthe Y. Choueiry (Sh u-w e-ri) (402)472-5444 Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
F eatures of Lisp 1. In tera tiv e: in terpreted and ompiled B.Y. 2. Sym b oli ✫ ✬ Choueiry 3. F un tional 4. Se ond oldest language but still `widely' used (Ema s, AutoCad, Ma Syma, Y aho o Store, Orbitz, et .) Soft w are/Hardw are W e ha v e Allegro Common Lisp (b y F ran In .): alisp and mlisp 2 There are man y old and new diale ts (CormanLisp, Ky oto CL, LeLisp, CMU CL, SBCL, ECL, Op enMCL, CLISP , et .) There ha v e also b een Lisp ma hines (Sym b oli s, Conne tion • Ma hine, IT Explorer, others?) • Instru tor's Jan uary • notes 20, 2012 #3 ✪ ✩
B.Y. Lisp as a fun tional language ✫ ✬ Choueiry (fun tion-name arg1 arg2 et ) 1. Ev aluate argumen ts 2. ev aluate fun tion with argumen ts 3. return the result 3 F un tions as argumen ts to other fun tions: (name2 (name1 arg1 arg2 et ) arg3 arg2 et ) Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
Sym b oli language A toms: n umeri atoms (n um b ers), sym b oli atoms (sym b ols) B.Y. Ea h sym b ol has: prin t-name, plist, pa k age, sym b ol-v alue, ✫ ✬ Choueiry sym b ol-fun tion • Lists: • (A B C) 4 NIL C A B (A (B C) D) NIL Sym b oli expressions: sym b ols and lists D Instru tor's A NIL Jan B C uary notes 20, 2012 #3 ✪ ✩
More onstru ts B.Y. Data t yp es: ✫ ✬ Choueiry 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 ols, ev aluate to self • Basi fun tions: first ( ar), rest ( dr), se ond, tenth setf : do es not ev aluate �rst argumen t ons, append, equal , op erations on sets, et . • 5 Basi ma ros: defun, defma ro, defstru t, def lass, defmethod, defvar, defparameter Instru tor's • Jan uary notes 20, 2012 #3 ✪ ✩
Sp e ial forms: let , let* , flet , labels , progn , B.Y. ✫ ✬ Predi ates: Choueiry listp , endp , atom , numberp , symbolp , evenp , oddp , et . • Conditionals: if <test> <then form> <else form> , • when <test> <then form> , unless <test> <else form> , • ond , ase 6 Lo oping onstru ts: dolist , dotimes , do , map ar , loop , Lam b da fun tions Instru tor's • Jan uary notes • 20, 2012 #3 ✪ ✩
A really fun tional language B.Y. ✫ ✬ Choueiry (defun <function-name> <arg1> <arg2> <arg3> ... (flet ((local-function-name <arg a> <arg b> .... ........ <return some value>) ) (.............. 7 #’(lambda (x) ..........) ..) <some-value>)) defun, flet/labels, lambda Regular function Anonymous function Local function Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
What mak es Lisp di�eren t? B.Y. ✫ ✬ Par adigms of AI Pr o gr amming, Norvig Choueiry Built-in supp ort for lists Dynami storage managemen t (garbage olle tion!) Dynami t yping • First- lass fun tions (dynami ally reated, anon ymous) • Uniform syn tax 8 • In tera tiv e en vironmen t • Extensibilit y • • Instru tor's Jan • uary notes 20, 2012 #3 ✪ ✩
Allegro Common Lisp B.Y. ✫ ✬ Choueiry F ree do wnload: www.franz. om/downl oad s/ A v ailable on SunOS ( s e.unl.edu), and Lin ux. Great in tegration with ema s • Che k www.franz. om/ema s/ Che k ommands distributed b y instru tor • • 9 Great dev elopmen t en vironmen t Comp oser: debugger, insp e tor, time/spa e pro�ler, et . (require ' omposer) • Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
;;; -*- Pa kage: USER; Mode: LISP; Base: 10; Syntax: Common-Lisp -*- B.Y. ✫ ✬ Choueiry (in-pa kage "USER") ;;;; +==================== === == === === === === === === === === === === ==+ ;;;; | Sour e ode for the farmer, wolf, goat, abbage problem | ;;;; | from Luger's "Artifi ial Intelligen e, 4th Ed." | ;;;; | In order to exe ute, run the fun tion CROSS-THE-RIVER | 10 ;;;; +==================== === == === === === === === === === === === === ==+ Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
✬ ✩ ;;; +================== === === == === === === === === === =+ ;;; | State definitions and asso iated predi ates | ;;; +================== === === == === === === === === === =+ (defun make-state (f w g ) (list f w g )) (defun farmer-side (state) (nth 0 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 ✫ ✪
;;; +================== === =+ ;;; | Operator definitions | ;;; +================== === =+ B.Y. ✫ ✬ (defun farmer-takes-self (state) Choueiry (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)) 12 (opposite (wolf-side state)) (goat-side state) ( abbage-side state)))) (t nil))) Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
(defun farmer-takes-goat (state) ( ond ((equal (farmer-side state) (goat-side state)) B.Y. ✫ (safe (make-state (opposite (farmer-side state)) ✬ Choueiry (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)) 13 (wolf-side state) (goat-side state) (opposite ( abbage-side state))))) (t nil))) Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
;;; +================== =+ ;;; | Utility fun tions | ;;; +================== =+ B.Y. ✫ ✬ Choueiry (defun opposite (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) 14 ((and (equal (goat-side state) ( abbage-side state)) (not (equal (farmer-side state) (goat-side state)))) nil) (t state))) Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
;;; +========+ B.Y. ;;; | Sear h | ✫ ✬ Choueiry ;;; +========+ (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)) 15 (path (farmer-takes-goat state) goal ( ons state been-list)) (path (farmer-takes- abb age state) goal ( ons state been-list))) ))) Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
B.Y. ✫ ✬ ;;; +================== + Choueiry ;;; | 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))) 16 Instru tor's Jan uary notes 20, 2012 #3 ✪ ✩
Recommend
More recommend