✬ ✩ cs. (402)472-5444 CSCE 476-876, Fall 2017 A little bit of Lisp Berthe Y. Choueiry (Shu-we-ri) Introduction to Artificial Intelligence www.cse.unl.edu/~choueiry/F17-476-876 Read LWH: Chapters 1, 2,Every 3, recitation and (Monday): 4. ask your questions on Lisp/xema

B.Y. Choueiry 1 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ We have Allegro (by FrancThere Inc.): are alisp many and old mlisp andLeLisp, new CMU dialects CL, (CormanLisp, SBCL, Kyoto ECL, CL, OpenMCL,There CLISP, have etc.) also been LispMachine, machines IT (, Explorer, Connection others?) (Emacs, AutoCad, MacSyma, Yahoo Store, Orbitz, etc.) • • • 1. Interactive: interpreted and compiled 2. Symbolic 3. Functional 4. Second oldest language but still ‘widely’ used Software/Hardware Features of Lisp

B.Y. Choueiry 2 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ ) etc ) ) arg3 arg2 etc etc 1. Evaluate arguments 2. evaluate function with arguments 3. return the result Lisp as a functional language (function-name arg1 arg2 Functions as arguments to other(name2 functions: (name1 arg1 arg2

B.Y. Choueiry 3 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ , NIL NIL NIL C D C B AB A (A B C) (A (B C) D) Atoms: numeric atoms (numbers), symbolicEach atoms symbol (symbols) has: print-name, plist,symbol-function package, symbol-value Lists: • • Symbolic language Symbolic expressions: symbols and lists

B.Y. Choueiry 4 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ ), etc. . etc , operations on sets, : does not evaluate first argument Data types: atoms and lists, packages, strings,bit-vectors, structures, arrays, vectors, streams, hash-tables, classesNIL, (CLOS , numbers, strings: special symbols,Basic evaluate functions: to self first (car), restsetf (cdr), second, tenth cons, append, equal Basic macros: defun, defmacro, defstruct,defvar, defclass, defparameter defmethod, • • • More constructs

B.Y. Choueiry 5 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ . etc , oddp , , evenp , , , loop , symbolp , , , progn , mapcar , numberp do , labels , , atom , flet , dotimes endp , , let* , , when case Looping constructs: dolist Conditionals: if Lambda functions Predicates: listp Special forms: let • • • • •

B.Y. Choueiry 6 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ Local function Anonymous function #'(lambda (x) ...... ) ..) ) (...... )) (flet ((local-function-name b> ...... ) (defun ... Regular function A really functional language defun, flet/labels, lambda

B.Y. Choueiry 7 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ Paradigms of AI Programming, Norvig Built-in support for lists Dynamic storage management (garbage collection!) Dynamic typing First-class functions (dynamically created, anonymous) Uniform syntax Interactive environment Extensibility • • • • • • • What makes Lisp different?

B.Y. Choueiry 8 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ Check commands distributed www.franz.com/downloads/ www.franz.com/emacs/ Free download: Available on SunOS (csce.unl.edu), and Linux. Great integration with emacs Check by instructor Great development environment Composer: debugger, inspector, time/space(require profiler, etc. ’composer) • • • •

B.Y. Choueiry 9 Instructor’s notes #3 ✫ August 28, 2017✪ |

✬ m | ✩ Lisp -*- ======+ ======+ ;;; -*- Package: USER; Mode: LISP; Base: 10; Syntax: Common- (in-package "USER") ;;;; +======;;;; | Source;;;; code | for the;;;; farmer, | wolf, goat,;;;; In from cabbage +======order Luger’s proble to "Artificial execute, Intelligence, run 4th the Ed." function CROSS-THE-RIVER |

B.Y. Choueiry 10 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ (list f w g c)) (nth 0 state)) (nth 1 state)) (nth 2 state)) (nth 3 state)) ;;; +======+ ;;; | State;;; definitions +======+ and associated(defun predicates make-state | (f w g c) (defun farmer-side (state) (defun wolf-side (state) (defun goat-side (state) (defun cabbage-side (state)

B.Y. Choueiry 11 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ (opposite (wolf-side state)) (goat-side state) (cabbage-side state)))) (wolf-side state) (goat-side state) (cabbage-side state))) (safe (make-state (opposite (farmer-side state)) (t nil))) (cond ((equal (farmer-side state) (wolf-side state)) (make-state (opposite (farmer-side state)) (defun farmer-takes-wolf (state) ;;; +======+ ;;; | Operator;;; definitions +======+ | (defun farmer-takes-self (state)

B.Y. Choueiry 12 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ (wolf-side state) (goat-side state) (opposite (cabbage-side state))))) (wolf-side state) (opposite (goat-side state)) (cabbage-side state)))) (safe (make-state (opposite (farmer-side state)) (t nil))) (safe (make-state (opposite (farmer-side state)) (t nil))) (cond ((equal (farmer-side state) (cabbage-side state)) (cond ((equal (farmer-side state) (goat-side state)) (defun farmer-takes-cabbage (state) (defun farmer-takes-goat (state)

B.Y. Choueiry 13 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ (not (equal (farmer-side state) (goat-side state)))) (not (equal (farmer-side state) (wolf-side state)))) nil) nil) (t state))) ((and (equal (goat-side state) (cabbage-side state)) ((equal side ’w) ’e))) (cond ((and (equal (goat-side state) (wolf-side state)) (cond ((equal side ’e) ’w) (defun safe (state) ;;; +======+ ;;; | Utility;;; functions +======+ | (defun opposite (side)

B.Y. Choueiry 14 Instructor’s notes #3 ✫ August 28, 2017✪ list))) t)) t))

✬ list)) ✩ (path (farmer-takes-wolf state)(path goal (farmer-takes-goat (cons state) state(path goal been-lis (farmer-takes-cabbage (cons state) state goal been-lis (cons state been- ))) (or (path (farmer-takes-self state) goal (cons state been- ((null state) nil) ((equal state goal)((not (reverse (member (cons state state been-list been-list))) :test #’equal)) (cond ;;; +======+ ;;; | Search;;; | +======+ (defun path (state goal &optional (been-list nil))

B.Y. Choueiry 15 Instructor’s notes #3 ✫ August 28, 2017✪ ✬ ✩ (goal (make-state ’w ’w ’w ’w))) (path start goal))) (let ((start (make-state ’e ’e ’e ’e)) ;;; +======+ ;;; | Canned;;; Execution +======+ | (defun cross-the-river ()

B.Y. Choueiry 16 Instructor’s notes #3 ✫ August 28, 2017✪