Choueiry In tro dution to Artiial In telligene CSCE - - PowerPoint PPT Presentation

choueiry in tro du tion to arti ial in telligen e csce
SMART_READER_LITE
LIVE PREVIEW

Choueiry In tro dution to Artiial In telligene CSCE - - PowerPoint PPT Presentation

A little bit of Lisp B.Y. Choueiry In tro dution to Artiial In telligene 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


slide-1
SLIDE 1

✬ ✫ ✩ ✪

A little bit
  • f
Lisp 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
  • ur
questions
  • n
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
SLIDE 2

✬ ✫ ✩ ✪

F eatures
  • f
Lisp 1. In tera tiv e: in terpreted and
  • mpiled
2. Sym b
  • li
3. F un tional 4. Se ond
  • ldest
language but still `widely' used (Ema s, AutoCad, Ma Syma, Y aho
  • Store,
Orbitz, et .) Soft w are/Hardw are
  • W
e ha v e Allegro Common Lisp (b y F ran In .): alisp and mlisp
  • There
are man y
  • ld
and new diale ts (CormanLisp, Ky
  • to
CL, LeLisp, CMU CL, SBCL, ECL, Op enMCL, CLISP , et .)
  • There
ha v e also b een Lisp ma hines (Sym b
  • li s,
Conne tion Ma hine, IT Explorer,
  • thers?)
B.Y. Choueiry 2 Instru tor's notes #3 Jan uary 20, 2012
slide-3
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
  • ther
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
SLIDE 4

✬ ✫ ✩ ✪

Sym b
  • li
language
  • A
toms: n umeri atoms (n um b ers), sym b
  • li
atoms (sym b
  • ls)
Ea h sym b
  • l
has: prin t-name, plist, pa k age, sym b
  • l-v
alue, sym b
  • l-fun tion
  • Lists:

(A B C) NIL (A (B C) D) A NIL NIL D B C A B C

Sym b
  • li
expressions: sym b
  • ls
and lists B.Y. Choueiry 4 Instru tor's notes #3 Jan uary 20, 2012
slide-5
SLIDE 5

✬ ✫ ✩ ✪

More
  • nstru ts
  • Data
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
  • ls,
ev aluate to self
  • Basi
fun tions: first ( ar), rest ( dr), se ond, tenth setf: do es not ev aluate rst argumen t
  • ns,
append, equal,
  • p
erations
  • n
sets, et .
  • Basi
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
SLIDE 6

✬ ✫ ✩ ✪

  • Sp
e ial forms: let, let*, flet, labels, progn,
  • Predi ates:
listp, endp, atom, numberp, symbolp, evenp,
  • ddp,
et .
  • Conditionals:
if <test> <then form> <else form>, when <test> <then form>, unless <test> <else form>,
  • nd,
ase
  • Lo
  • ping
  • nstru ts:
dolist, dotimes, do, map ar, loop,
  • Lam
b da fun tions B.Y. Choueiry 6 Instru tor's notes #3 Jan uary 20, 2012
slide-7
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
SLIDE 8

✬ ✫ ✩ ✪

What mak es Lisp dieren t? Par adigms
  • f
AI Pr
  • gr
amming, Norvig
  • Built-in
supp
  • rt
for lists
  • Dynami
storage managemen t (garbage
  • lle tion!)
  • Dynami
t yping
  • First- lass
fun tions (dynami ally reated, anon ymous)
  • Uniform
syn tax
  • In
tera tiv e en vironmen t
  • Extensibilit
y B.Y. Choueiry 8 Instru tor's notes #3 Jan uary 20, 2012
slide-9
SLIDE 9

✬ ✫ ✩ ✪

Allegro Common Lisp
  • F
ree do wnload: www.franz. om/downl
  • ad
s/
  • A
v ailable
  • n
SunOS ( s e.unl.edu), and Lin ux.
  • Great
in tegration with ema s Che k www.franz. om/ema s/ Che k
  • mmands
distributed b y instru tor
  • Great
dev elopmen t en vironmen t Comp
  • ser:
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
SLIDE 10

✬ ✫ ✩ ✪

;;;
  • *-
Pa kage: USER; Mode: LISP; Base: 10; Syntax: Common-Lisp
  • *-
(in-pa kage "USER") ;;;; +==================== === == === === === === === === === === === === ==+ ;;;; | Sour e
  • de
for the farmer, wolf, goat, abbage problem | ;;;; | from Luger's "Artifi ial Intelligen e, 4th Ed." | ;;;; | In
  • rder
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
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
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
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
SLIDE 14

✬ ✫ ✩ ✪

;;; +================== =+ ;;; | Utility fun tions | ;;; +================== =+ (defun
  • pposite
(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
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
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