
B.Y. Choueiry ✫ ✬ A little bit of Lisp Introduction to Artificial Intelligence CSCE 476-876, Fall 2017 1 www.cse.unl.edu/~choueiry/F17-476-876 Read LWH: Chapters 1, 2, 3, and 4. Every recitation (Monday): ask your questions on Lisp/xemacs. Instructor’s notes #3 August 28, 2017 Berthe Y. Choueiry (Shu-we-ri) (402)472-5444 ✪ ✩ B.Y. Choueiry ✫ ✬ Features of Lisp 1. Interactive: interpreted and compiled 2. Symbolic 3. Functional 4. Second oldest language but still ‘widely’ used (Emacs, AutoCad, MacSyma, Yahoo Store, Orbitz, etc.) 2 Software/Hardware • We have Allegro Common Lisp (by Franc Inc.): alisp and mlisp Instructor’s notes #3 • There are many old and new dialects (CormanLisp, Kyoto CL, August 28, 2017 LeLisp, CMU CL, SBCL, ECL, OpenMCL, CLISP, etc.) • There have also been Lisp machines (Symbolics, Connection Machine, IT Explorer, others?) ✪ ✩ B.Y. Choueiry ✫ ✬ Lisp as a functional language (function-name arg1 arg2 etc) 1. Evaluate arguments 3 2. evaluate function with arguments 3. return the result Instructor’s notes #3 Functions as arguments to other functions: August 28, 2017 (name2 (name1 arg1 arg2 etc) arg3 arg2 etc) ✪ ✩ B.Y. Choueiry ✫ ✬ Symbolic language • Atoms: numeric atoms (numbers), symbolic atoms (symbols) Each symbol has: print-name, plist, package, symbol-value, symbol-function • Lists: (A B C) 4 NIL AB C (A (B C) D) NIL Instructor’s notes #3 D A NIL August 28, 2017 B C Symbolic expressions: symbols and lists ✪ ✩ B.Y. Choueiry ✫ ✬ More constructs • Data types: atoms and lists, packages, strings, structures, vectors, bit-vectors, arrays, streams, hash-tables, classes (CLOS), etc. NIL, T, numbers, strings: special symbols, evaluate to self 5 • Basic functions: first (car), rest (cdr), second, tenth setf: does not evaluate first argument cons, append, equal, operations on sets, etc. Instructor’s notes #3 • Basic macros: August 28, 2017 defun, defmacro, defstruct, defclass, defmethod, defvar, defparameter ✪ ✩ B.Y. Choueiry ✫ ✬ • Special forms: let, let*, flet, labels, progn, • Predicates: listp, endp, atom, numberp, symbolp, evenp, oddp, etc. • Conditionals: if <test> <then form> <else form>, 6 when <test> <then form>, unless <test> <else form>, cond, case Instructor’s notes #3 • Looping constructs: August 28, 2017 dolist, dotimes, do, mapcar, loop, • Lambda functions ✪ ✩ B.Y. Choueiry ✫ ✬ A really functional language (defun <function-name> <arg1> <arg2> <arg3> ... (flet ((local-function-name <arg a> <arg b> .... ........ <return some value>) ) 7 (.............. #'(lambda (x) ..........) ..) <some-value>)) Regular function Anonymous function Instructor’s notes #3 Local function August 28, 2017 defun, flet/labels, lambda ✪ ✩ B.Y. Choueiry ✫ ✬ What makes Lisp different? Paradigms of AI Programming, Norvig • Built-in support for lists • Dynamic storage management (garbage collection!) 8 • Dynamic typing • First-class functions (dynamically created, anonymous) • Uniform syntax Instructor’s notes #3 • Interactive environment August 28, 2017 • Extensibility ✪ ✩ B.Y. Choueiry ✫ ✬ Allegro Common Lisp • Free download: www.franz.com/downloads/ • Available on SunOS (csce.unl.edu), and Linux. 9 • Great integration with emacs Check www.franz.com/emacs/ Check commands distributed by instructor Instructor’s notes #3 • Great development environment August 28, 2017 Composer: debugger, inspector, time/space profiler, etc. (require ’composer) ✪ ✩ B.Y. Choueiry ✫ ✬ ;;; -*- Package: USER; Mode: LISP; Base: 10; Syntax: Common-Lisp -*- (in-package "USER") 10 ;;;; +=========================================================+ ;;;; | Source code for the farmer, wolf, goat, cabbage problem | ;;;; | from Luger’s "Artificial Intelligence, 4th Ed." | ;;;; | In order to execute, run the function CROSS-THE-RIVER | Instructor’s notes #3 ;;;; +=========================================================+ August 28, 2017 ✪ ✩ B.Y. Choueiry ✫ ✬ ;;; +=============================================+ ;;; | State definitions and associated predicates | ;;; +=============================================+ (defun make-state (f w g c) (list f w g c)) (defun farmer-side (state) 11 (nth 0 state)) (defun wolf-side (state) (nth 1 state)) Instructor’s notes #3 (defun goat-side (state) August 28, 2017 (nth 2 state)) (defun cabbage-side (state) (nth 3 state)) ✪ ✩ B.Y. Choueiry ✫ ✬ ;;; +======================+ ;;; | Operator definitions | ;;; +======================+ (defun farmer-takes-self (state) (make-state (opposite (farmer-side state)) (wolf-side state) (goat-side state) 12 (cabbage-side state))) (defun farmer-takes-wolf (state) (cond ((equal (farmer-side state) (wolf-side state)) (safe (make-state (opposite (farmer-side state)) Instructor’s notes #3 (opposite (wolf-side state)) August 28, 2017 (goat-side state) (cabbage-side state)))) (t nil))) ✪ ✩ B.Y. Choueiry ✫ ✬ (defun farmer-takes-goat (state) (cond ((equal (farmer-side state) (goat-side state)) (safe (make-state (opposite (farmer-side state)) (wolf-side state) (opposite (goat-side state)) (cabbage-side state)))) (t nil))) 13 (defun farmer-takes-cabbage (state) (cond ((equal (farmer-side state) (cabbage-side state)) (safe (make-state (opposite (farmer-side state)) Instructor’s notes #3 (wolf-side state) (goat-side state) August 28, 2017 (opposite (cabbage-side state))))) (t nil))) ✪ ✩ B.Y. Choueiry ✫ ✬ ;;; +===================+ ;;; | Utility functions | ;;; +===================+ (defun opposite (side) (cond ((equal side ’e) ’w) ((equal side ’w) ’e))) 14 (defun safe (state) (cond ((and (equal (goat-side state) (wolf-side state)) (not (equal (farmer-side state) (wolf-side state)))) nil) Instructor’s notes #3 ((and (equal (goat-side state) (cabbage-side state)) August 28, 2017 (not (equal (farmer-side state) (goat-side state)))) nil) (t state))) ✪ ✩ B.Y. Choueiry ✫ ✬ ;;; +========+ ;;; | Search | ;;; +========+ (defun path (state goal &optional (been-list nil)) (cond 15 ((null state) nil) ((equal state goal) (reverse (cons state been-list))) ((not (member state been-list :test #’equal)) (or (path (farmer-takes-self state) goal (cons state been-list)) (path (farmer-takes-wolf state) goal (cons state been-list)) Instructor’s notes #3 (path (farmer-takes-goat state) goal (cons state been-list)) August 28, 2017 (path (farmer-takes-cabbage state) goal (cons state been-list))) ))) ✪ ✩ ✬ ✩ (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✪.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages16 Page
-
File Size-