Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham article) Because it s got lots of features other languages don t Because you can write new programs and etend old programs really, really quickly in Lisp Lisp stands for LISt Process Invented by John McCarthy (5) Simple data structure (atoms and lists) Heavy use of recursion Interpretive language Variations Frantz Lisp (0 s) Common Lisp (de facto industrial standard) Common Lisp at glumbcedu and sunservercseeumbcedu command line: clisp main site: http://clispsourceforgenet/ help site: http://wwwapljhuedu/~hall/lisphtml tutorial site: http://grimpeurtamuedu/~colin/lp/node0html Valid objects (S-epressions) Atoms: numbers: (real 0, integer ) symbols: a consecutive sequence of characters (no space) eg, a,, price-of-beef two special symbols: and for logical true and false strings: a sequence of characters bounded by double quotes eg, "this is red" (Note: LISP is case insensitive) Lists: a list of atoms and/or lists, bounded by "(" and "), eg, (a b c), (a (b c top elements of a list eample: top elements of list (a b c) are a, b, and c top elements of list (a (b c are a and (b c) nil: empty list, same as () Function calls also a list use prefi notation: (function-name arg argn) returns function value for the given list of arguments functions are either provided by Lisp function library or defined by the user Eamples: >(+ 5) >(/ 5) /5 >(/ 5) 05 >(sqrt 4) Evaluation of S-epression ) Evaluate an atom numerical and string atoms evaluate to themselves; symbols evaluate to their values if they are assigned values, return Error, otherwise; the values of and are themselves ) Evaluate a list - evaluate every top element of the list as follows, unless eplicitly forbidden: the first element is always a function name; evaluating it means to call the function body; each of the rest elements will then be evaluated, and their values returned as the arguments for the function Eamples >(+ (/ 5) 4) /5 >(+ (sqrt 4) 40) 0 >(sqrt ) Error: he variable X is unbound
) o assign a value to a symbol (setq, set, setf) >(setq ) > setq is a special form of function (with two arguments); the first argument is a symbol which will not be evaluated; the second argument is a S-epression, which will be evaluated; the value of the second argument is assigned to be the value of the first argument >(setq y ) ; the value of is assigned as the value of y >y to forbid evaluation of a symbol (quote or ) >(+ y) 0 >(quote ) >' >(+ z) Error: X is not of type NUMBER >(setq z ') to force an evaluation, using function "eval" >(+ (eval z 0 wo more assignment functions: (set y) ; assign the value of y to the value of is evaluated ; first and whose value must be a symbol ; "setq" is a combination of "set" and "quote" (setf y) ; similar to but more general than "setq" in that can be ; something other than a symbol 4 Basic LISP functions ) list operations: car and cdr >(setq L '(a b c (a b c) ; assigns a list (a b c) as the value of L >(cadr L) B ; car of cdr of L >(car L) A ; returns the first top level element of list L >(cddr L) (C) >(cdddr L) (nth i L) returns the ith top element of L (the front element is considered 0th element) >(cdr L) (B C) ; returns the rest of list L >(caddr L) C >(cadddr L) >(nth L) C other list operations >(cons ' L) ; insert symbol at the front of list L (X A B C) >(list 'a 'b 'c) ; making a list with the arguments as its elements ; if a, b, c have values, y, z, then (list a b c) ; returns list ( y z) >(append '(a b) '(c d (A B C D) ; appends one list in front of another >(reverse L) ; reverses a list (C B A) >(length L) ; returns the length of list L ) Predicates (a special function which returns if the predicate is false, or anything other than, otherwise) =, >, <, >=, <= for numerical values; equal, eq, for others (symbols, lists, etc) >(< y) tests if is a atom tests if is a list >(= y) >(listp ) also numberp, symbolp, null >(numberp ) >(equal y) >(atom ) >(numberp ) >(atom L) >(listp L) >(symbolp ) >(equal a (car L >(atom (car L >(symbolp ) >(null L) >(null ) >(null ) ) Set operations ( a list can be viewed as a set whose members are the top elements of the list) >(member 'b L) ; test if symbol b is a member (a top element) of L (B C) ; if yes, returns the sublist of L starting at the ; first occurrence of symbol b >(member b (cons 'b L (B A B C) >(member L) ; if no, returns >(union L L) ; returns the union of the two lists >(intersection L L) ; returns the intersection of the two lists >(set-difference L L) ; returns the difference of the two lists
4) Conditional >(cond (<test-> <action->) (<test-k> <action-k> each (<test-i> <action-i>) is called a clause; if test-i (start with i=) returns (or anything other than ), this function returns the value of action-i; else, go to the net clause; usually, the last test is, which always holds, meaning otherwise cond can be nested (action-i may contain (cond 5 Define functions (heavy use of recursive definitions) (defun func-name (arg- Arg-n) func-body) eamples: (defun member ( L) (cond ((null L) nil) ; base case : L is empty ((equal (car L L) ; base case : =first(l) (t (member (cdr L) ; recursion: test if is in rest(l) (defun intersection (L L) (cond ((null L) nil) ((null L) nil) ((member (car L) L) (cons (car L) (intersection (cdr L) L) (t (intersection (cdr L) L Eample: (intersection '(a b c) '(b a b c returns (a b c) (intersection '(b a b c) '(a b c returns (b a b c) (defun set-difference (L L) (cond ((null L) nil) ((null L) L) ((not (member (car L) L (cons (car L) (set-difference (cdr L) L) (t (set-difference (cdr L) L Define functions iteratively (dolist ( L result) body) for each top level element in L, do body(); is not equal to an element of L in each iteration, but rather takes an element of L as its value; (dotimes (count n result) body) ; do body n times count starts with 0, ends with n- Note: result is optional, to be used to hold the computing result If result is given, the function will return the value of result, returns, otherwise (may change global variables as side effects) (defun sum (L) (dolist ( L y) (setq y (+ y (defun sum (L) (dotimes (count (length L) y) (setq y (+ y (nth count L) (defun sum (L) (dolist ( L y) (setq y (+ y (eval ) defun sum4 (L) (dotimes (count (length L) y) (setq y (+ y (eval (nth count L >(setq L '( ( ) >(dotimes (count ) (set (nth count L) (nth count L) >(sum L) >(sum L) >(setq L '(a b c >(sum L) Error: >(sum L) Error: >(sum L) >(sum4 L) Other functions in LISP library ) Predicates:zerop, plusp, evenp, oddp, integerp, floatp ) Logical connector: and, or, not ) Rounding: floor,ceiling, truncate, round 4) Others: ma, min, abs, sqrt, + (add ), - (minus ) (ep number) (base-e eponential) (ept Base-number Power-Number) (log number & Optional base-number) (isqrt number) Returns the greater integer less than or equal to the eact positive square-root of the number (signum number) Returns -, zero, or according if the number is negative, zero, or positive
7 Other features ) Property lists: Assign/access properties (attribute-value pairs) of a symbol o assign a property: (setf (get object attribute) value) o obtain a property: (get object attribute) Eample: >(setf (get 'a 'heights) ) ; cannot use "setq" here >(get 'a 'height) >(setf (get (cadr L) 'height) ) >(get 'b 'height) ) Associative list: attach a list of properties to a symbol, each property can be retrieved by key (property symbol) >(setf sarah '((height ) (weight 00) (se "F") ((HEIGH ) (WEIGH 00) (SEX "F" >(assoc 'weights sarah) (WEIGH 00) ) mapcar: (mapcar # p-name L) transform list L to another list by performing procedure p-name to each top level element of L >(mapcar # sqrt L) ( 445 7050) (defun sq () (* >(mapcar # sq L) ( 4 ) >(mapcar # set L L) ( ) >(mapcar #'* L L L) ( 7) transforming more than one lists define the function within mapcar (unnamed), use lambda notation >(mapcar #'(lambda () (setq (+ (eval L) ( 4) 4) input/output: print/read on screen: >(print (get 'a 'height >(print L) >(setq p (read 0 ;typed on the screen 0 >p 0 with eternal file: (with-open-file (<stream-name> <file-name> :direction :input or :output) ) internal variable name eternal file name >(with-open-file (data "indat" :direction :input) ; input file indat contains (setq L nil) ; 4 5 (dotimes (count 5) (setq L (cons (read data) L) ) >L (5 4 ) >(with-open-file (result "outdat" :direction :output) (dotimes (count 5) (print (+ (nth count L result) ;an eternal file "outdat" is created and contains 5 4 5) Some new primitive/functions Access a list first, second,, tenth ;etension of CAR, ;return the ith element rest, last ; etension of CDR, return a list Conditional (if <test> body body) (when <test> body) (unless <test> body) ;do body if test is true, ;body, otherwise ;do body when test is true ;do body when test is false 4
) Miscellaneous %clisp >(bye) or (quit) or <ctrl>-d (load "file-name") (ed "file-name") ; enter Common Lisp of CMU (on glumbcedu) ; eit CLISP ; load in a file ; enter vi editor (compile-file "file-name") ; the compiled version is in file-nameo ; then load in file-nameo (compile 'func-name) ; compile a particular function (time (func-name arg argn ; print real and run time for eecuting func-name Summary Atoms and lists Functions and function calls setq, setf, set, quote, eval, math functions (+, -, *, /, ma, min, ep, sqrt, ) list operations: list, cons, car, cdr, length, nth, append, reverse predicates (=, >, equal, eq, numberp, symbolp, ) Defining functions (defun func_name (arg_list) func_body) dolist, dotimes, cond, if, when, unless, mapcar Properties and associative lists: get, assoc Input/output: print, read, with-open-file, load 5