Intro to LISP Walkthru
MCS 380/590 Intro AI, Fall 2000 Chris Cera Luiza Helena da Silva
UCLA AI FAQ | ftp://ftp.cs.ucla.edu/pub/AI/ |
CMU AI Repository | http://www.cs.cmu.edu/Groups/AI/html/repository.html |
Paul Graham's website | http://www.paulgraham.com/ |
Peter Norvig's website | http://www.norvig.com |
Stewart Russell's website | http://www.cs.berkeley.edu/~russell/ |
Philosophical Foundations of AI | http://sern.ucalgary.ca/courses/CPSC/533/W99/presentations/L2_26_Haines_Jurado/main.html |
Allegro Common LISP | http://www.franz.com also on Queen machines in MCS lab. This is text terminal based. |
Harlequin LISPworks | http://www.harlequin.com |
gcl (GNU Common LISP) | http://www.gnu.org/software |
CLISP (ANSI Common LISP) | http://www.gnu.org/software |
Some examples in this introduction are done using Harlequin LISPworks for Linux, and other examples are done using Allegro Common Lisp 5 for Linux.
CL-USER 0 > |
Whenever you execute an illegal instruction the debugger halts the interpreter and jumps into the debugger.
A debugger is not part of Common LISP, so may not work on all implementations. Another 'exit' command is possible among different interpreters. That really goes for all commands being implementation specific since it is not covered in any standard.
CL-USER 15 > (* 3 4) ;; this is a LISP comment 12 CL-USER 16 > (* 3 4) # this text is invalid Error: Dotted list in APPLY: COMPLEX to OMMENT. 1 (abort) Return to level 0. 2 Return to top loop level 0. Type :b for backtrace, :c <option number> to proceed, or :? for other options CL-USER 16 : 1 > :c Cannot continue. NIL CL-USER 17 : 1 > :c 2 CL-USER 18 > CL-USER 19 > (1+ 2 3) Error: Call (# |
USER(82): c Error: Attempt to take the value of the unbound variable `C'. [condition type: UNBOUND-VARIABLE] Restart actions (select using :continue): 0: Try evaluating C again. 1: Use :C instead. 2: Set the symbol-value of C and use its value. 3: Use a value without setting C. 4: Return to Top Level (an "abort" restart) 5: Abort #<PROCESS Initial Lisp Listener> [1] USER(83): :continue 4 USER(84): |
CL-USER 1 > (load "c:\\windows\\desktop\\aima\\aima.lisp") ; Loading text file c:\windows\desktop\aima\aima.lisp ; Loading text file c:\windows\desktop\aima\utilities\utilities.lisp ; Loading fasl file C:\PROGRAM FILES\HARLEQUIN\LISPWORKS PERSONAL\lib\4-1-0-0\modules\macros\defsetf.fsl Error: Redefining function TRUE visible from packages LISPWORKS. 1 (continue) Redefine it anyway. 2 Try loading c:\windows\desktop\aima\utilities\utilities again. 3 Give up loading c:\windows\desktop\aima\utilities\utilities. 4 Try loading another file instead of c:\windows\desktop\aima\utilities\utilities. 5 Try loading c:\windows\desktop\aima\aima.lisp again. 6 Give up loading c:\windows\desktop\aima\aima.lisp. 7 Try loading another file instead of c:\windows\desktop\aima\aima.lisp. 8 (abort) Return to level 0. 9 Return to top loop level 0. Type :b for backtrace, :c <option number> to proceed, or :? for other options CL-USER 2 : 1 > :c 1 Error: Redefining function FALSE visible from packages LISPWORKS. 1 (continue) Redefine it anyway. 2 Try loading c:\windows\desktop\aima\utilities\utilities again. 3 Give up loading c:\windows\desktop\aima\utilities\utilities. 4 Try loading another file instead of c:\windows\desktop\aima\utilities\utilities. 5 Try loading c:\windows\desktop\aima\aima.lisp again. 6 Give up loading c:\windows\desktop\aima\aima.lisp. 7 Try loading another file instead of c:\windows\desktop\aima\aima.lisp. 8 (abort) Return to level 0. 9 Return to top loop level 0. Type :b for backtrace, :c <option number> to proceed, or :? for other options CL-USER 3 : 1 > :c 1 ; Loading text file c:\windows\desktop\aima\utilities\binary-tree.lisp ; Loading text file c:\windows\desktop\aima\utilities\queue.lisp ; Loading text file c:\windows\desktop\aima\utilities\cltl2.lisp ; Loading text file c:\windows\desktop\aima\utilities\test-utilities.lisp |
USER(19): (exit) ; Exiting Lisp |
CL-USER 1 > ;; (8 + 3) * (4 + (9 * 2) ) CL-USER 2 > (* (+ 8 3) (+ 4 (* 9 2))) 242 |
CL-USER 3 > 8 8 CL-USER 4 > 3 3 |
CL-USER 44 > (a b c) Error: The variable B is unbound. 1 (continue) Try evaluating B again. 2 Return the value of :B instead. 3 Specify a value to use this time instead of evaluating B. 4 Specify a value to set B to. 5 (abort) Return to level 0. 6 Return to top loop level 0. Type :b for backtrace, :c <option number> to proceed, or :? for other options CL-USER 45 : 1 > :c 6 CL-USER 46 > (quote (a b c) ) (A B C) |
CL-USER 48 > (list 'a) (A) CL-USER 49 > (list 'a '(b c) 'd) (A (B C) D) |
CL-USER 36 > (+ 1 2) 3 CL-USER 37 > (+ 1 2 3) 6 |
CL-USER 38 > (1+ 10) 11 |
CL-USER 40 > (set 'x 5) 5 CL-USER 41 > (setq x 5) 5 CL-USER 42 > (+ x 1) 6 CL-USER 43 > (setq y '(a b c)) (A B C) |
CL-USER 47 > z Error: The variable Z is unbound. 1 (continue) Try evaluating Z again. 2 Specify a value to use this time instead of evaluating Z. 3 Specify a value to set Z to. 4 (abort) Return to level 0. 5 Return to top loop level 0. Type :b for backtrace, :c <option number> to proceed, or :? for other options |
USER(55): (setq x nil) NIL USER(56): (null x) T USER(57): (setq x '(a b c)) (A B C) USER(58): (null x) NIL |
USER(60): (atom x) T USER(61): (setq x '(a b c)) (A B C) USER(62): (atom x) NIL |
USER(65): (setq x 'a) A USER(66): (listp x) NIL USER(67): (setq x '(a b c)) (A B C) USER(68): (listp x) T USER(69): ; IMPORTANT NOTE (setq x nil) NIL USER(70): (listp x) T |
USER(69): (equal 'a 'b) NIL USER(70): (setq a 'b) B USER(71): (equal 'a 'b) NIL USER(72): (equal a 'b) T USER(73): (setq a '(x y z)) (X Y Z) USER(74): (setq b '(x y z)) (X Y Z) USER(75): (equal a b) T |
USER(76): (numberp 'a) NIL USER(77): (numberp '1) T |
USER(85): (typep 6 'number) T USER(86): (typep a 'number) NIL |
USER(90): (member 'a '(a b c)) (A B C) USER(91): (member 'd '(a b c)) NIL |
USER(4): (atom (car '(a b c))) T USER(5): (car '(a b c)) A |
USER(6): (atom (cdr '(a b c))) NIL USER(7): (cdr '(a b c)) (B C) |
USER(10): (listp (cons 'a '(b c))) T USER(11): (cons 'a '(b c)) (A B C) |
; b^2 - 4 a c
USER(1): (defun disc (a b c) (let ((d)) (setq d (- (* b b) (* 4 a c))))) DISC USER(2): (disc 1 3 4) -7 |
USER(12): (defun start_sqrt () (loop (print 'number>) (let ((in (read))) (if (equal in 'end) (return nil)) (prin1 (sqrt in))))) START_SQRT USER(13): (start_sqrt) NUMBER> 23 4.7958317 NUMBER> 45 6.708204 NUMBER> 47 6.8556547 NUMBER> 49 7.0 NUMBER> end NIL |
USER(3): (defun do-length (l) (do ((ll l (cdr ll)) (sum 0 (1+ sum))) ((atom ll) sum))) DO-LENGTH USER(4): (do-length '(a b c)) 3 |
USER(5): (defun prog-length (l) (prog ((sum 0)) again (cond ((atom l) (return sum))) (setq sum (1+ sum)) (setq l (cdr l)) (go again))) PROG-LENGTH USER(6): (prog-length '(a b c)) 3 |
(setf x '((a b) c d)) (setf y '(first (cdr (cdr x)))) |
x-------->[ | ]------------------->[ | ]---------------->[ |\] | | | | C /------------->D [ | ]->[ |\] | | | | A B | y------------------------------------------/
USER(19): (lambda (x) (* x x x)) #<Interpreted Function (unnamed) @ #x207bbc82> |
USER(29): (apply (lambda (x) (* x x x)) '(2)) 8 USER(30): (funcall (lambda (x) (* x x x)) 2) 8 |
USER(20): (defun cubed (x) (* x x x)) CUBED USER(21): (setf (symbol-function 'lambdacubed) '(lambda (x) (* x x x))) (LAMBDA (X) (* X X X)) USER(22): (cubed 3) 27 USER(23): (lambdacubed 3) 27 |
USER(33): (symbol-function 'cubed) #<Interpreted Function CUBED> |
USER(11): (time (do-length '(a b c))) ; cpu time (non-gc) 0 msec user, 0 msec system ; cpu time (gc) 0 msec user, 0 msec system ; cpu time (total) 0 msec user, 0 msec system ; real time 0 msec ; space allocation: ; 47 cons cells, 0 symbols, 32 other bytes, 0 static bytes 3 USER(12): (time (prog-length '(a b c))) ; cpu time (non-gc) 0 msec user, 0 msec system ; cpu time (gc) 0 msec user, 0 msec system ; cpu time (total) 0 msec user, 0 msec system ; real time 0 msec ; space allocation: ; 26 cons cells, 0 symbols, 32 other bytes, 0 static bytes 3 |
USER(1): (time (factorial 10)) ; cpu time (non-gc) 10 msec user, 0 msec system ; cpu time (gc) 0 msec user, 0 msec system ; cpu time (total) 10 msec user, 0 msec system ; real time 0 msec ; space allocation: ; 96 cons cells, 0 symbols, 352 other bytes, 0 static bytes 3628800 USER(2): (time (factorial 20)) ; cpu time (non-gc) 0 msec user, 0 msec system ; cpu time (gc) 0 msec user, 0 msec system ; cpu time (total) 0 msec user, 0 msec system ; real time 21 msec ; space allocation: ; 186 cons cells, 0 symbols, 864 other bytes, 0 static bytes 2432902008176640000 |
NOTE profiling the same function a second time may yield drastically low performance. This may be caused by some computers storing the program into RAM, and thus cutting down the runtime considerably.
In Linux
(defparameter *aima-root* (truename "/home/chris/class/380/myclass/AIMA/") ; < < < < < < < < Edit this < < < < < <
In Windows
(defparameter *aima-root* (truename "c:\\AIMA\\aima.lisp") ; < < < < < < < < Edit this < < < < < <
The actual lisp source code is platform independent. Except for one bug in linux listed below. I tested the AIMA code under two different interpreters in Linux, and one interpreter in Windows.
USER(3): (load "/home/chris/class/380/myclass/AIMA/aima.lisp") ; Loading /home/chris/class/380/myclass/AIMA/aima.lisp ; Loading /home/chris/class/380/myclass/AIMA/utilities/utilities.lisp ; Loading ; /home/chris/class/380/myclass/AIMA/utilities/binary-tree.lisp ; Loading /home/chris/class/380/myclass/AIMA/utilities/queue.lisp ; Loading /home/chris/class/380/myclass/AIMA/utilities/cltl2.lisp ; Loading ; /home/chris/class/380/myclass/AIMA/utilities/test-utilities.lisp T |
USER(1): (aima-compile) USER(2): (test 'all) |
USER(4): (aima-load 'all) USER(5): (test 'all) ... |
USER(50): (aima-load 'agents) USER(51): (test 'agents) ... ... USER(60): (aima-load 'search) USER(61): (test 'search) ... ... USER(70): (aima-load 'uncertainty) USER(71): (test 'uncertainty) ... ... USER(80): (aima-load 'language) USER(81): (test 'language) ... ... USER(90): (aima-load 'learning) USER(91): (test 'learning) ... ... USER(100): (aima-load 'logic) USER(101): (test 'logic) |
;(defmacro deletef (item sequence &rest keys &environment env) ; "Destructively delete item from sequence, which must be SETF-able." ; (multiple-value-bind (temps vals stores store-form access-form) ; (get-setf-method sequence env) ; (assert (= (length stores) 1)) ; (let ((item-var (gensym "ITEM"))) ; `(let* ((,item-var ,item) ; ,@(mapcar #'list temps vals) ; (,(first stores) (delete ,item-var ,access-form ,@keys))) ; ,store-form)))) |
The error that is generated as a result is:
;;; Compiling file /home/chris/class/380/CR/agents/algorithms/grid.lisp ; While compiling REMOVE-OBJECT: Error: attempt to call `GET-SETF-METHOD' which is an undefined function. [condition type: UNDEFINED-FUNCTION] Restart actions (select using :continue): 0: Try calling GET-SETF-METHOD again. 1: Return a value instead of calling GET-SETF-METHOD. 2: Try calling a function other than GET-SETF-METHOD. 3: Setf the symbol-function of GET-SETF-METHOD and call it again. 4: retry the compilation of /home/chris/class/380/CR/agents/algorithms/grid.lisp 5: continue compiling /home/chris/class/380/CR/agents/algorithms/grid.lisp but generate no output file 6: Return to Top Level (an "abort" restart) 7: Abort #<PROCESS Initial Lisp Listener> |