LISP OVERVIEW CMSC 471 -- FALL 2011 PROF. desJARDINS BASIC FUNCTIONS: (I solved all of the HW1 programming problems using a subset of these functions.) (defun FN (ARG1 ...) BODY) Define a new function named FN that takes arguments ARG1 ... and applies the function BODY to them (defun FN (ARG1 ... &optional (ARGk VALUE) BODY) Define a new function with optional argument ARGk that defaults to VALUE (defun FN (ARG1 ... &aux (ARGk VALUE) BODY) Define a new function with local variable ARGk that defaults to VALUE (setq X Y) Set the variable named X to the result of evaluating the expression Y. e.g., (setq x (+ 1 2)) sets the variable x equal to 3. (set X Y) Set the variable referred to by *evaluating* X to the result of evaluating the expression Y. e.g., (set 'x (+ 1 2)) sets the variable x equal to 3. (setf X Y) Set the generalized variable (could be a location of a list, a slot in a data structure, etc.) to the result of evaluating the expression Y. e.g., the pair of expressions (setf x '(1 2 3)) (setf (cadr x) 4) sets x to '(1 4 3). (eq X Y) Return T if X is equal to Y (i.e., if they evaluate to the same value -- also see eql, equal) (and X Y ...) If a sequence of evaluated expressions are all non-NIL, return the last of them; else, return NIL. Short-circuits (i.e., if any expression evaluates to NIL, the rest are not evaluated) (or X Y ...) If any of a sequence of evaluated expressions is non-NIL, return the first such expression. Also short-circuits, so does not evaluate any expressions after the first non-NIL expression. (integerp X) Returns T if X is an integer (see also listp, atom, null) (< X Y) Returns T if X is less than Y -- will cause an error if either X or Y evaluate to non-numbers. See also <=, >, >= (+ X Y) Returns the sum of X and Y (can take more than two arguments). See also *, -, /. (length LIST) Return the length of list L (car LIST) Return the first item of list L (could itself be a list) (cdr LIST) Return the "rest" of list L (i.e., the list created by removing the first item -- car -- of the list). Could return NIL if LIST only contains one item. (pop LIST) Change the variable LIST to refer to (cdr LIST), and return the car (first item) of the list. (cons VALUE LIST) Return a new list, of which VALUE is the first item, and LIST is the rest of the list. (nconc LIST1 LIST2) Destructively combine lists LIST1 and LIST2 into a new list that appends the elements of LIST2 at the end of LIST1. (Note that the final pointer of LIST1 now points to LIST2, hence the "destructive" modification of LIST1.) (append LIST1 LIST2) Create a new list that contains the elements of LIST1 followed by the elements of LIST2. Does not destructively modify LIST1 -- this function creates a new list structure. (mapcar #'FN LIST) Apply FN to each element of LIST and return a list of the results. (Also see mapcan, mapcdr, map, ...) E.g., (apply #'car '((a b) (c d) (e f))) returns '(a c e). (mapcar #'(lambda(ARG ...) BODY) LIST) Apply a "lambda function" (created on the fly) to LIST and return a list of the results. This is functional programming at its finest! (apply #'FN LIST) Apply FN to a list of arguments given in LIST; e.g., (apply #'+ '(1 2 3)) returns 6. (if EXPR BODY1 BODY2) If EXPR evaluates to non-NIL, evaluate BODY1; else evaluate BODY2. (cond ((COND1 BODY ...) (COND2 BODY ...) ... (T BODY ...))) Lisp's version of a CASE or extended IF...THEN...ELSE statement - the BODY associated the first COND that is T is evaluated (and no others); if no CONDs match, then of course T will be T, and that BODY will be evaluated. (loop KEYWORD ARGUMENT ...) Common Lisp's loop macro, with more options than I even know about. Useful keywords and arguments: while EXPR - keep looping as long as EXPR is true until EXPR - keep looping until EXPR is true do BODY - evaluate BODY (and return the value of the last such BODY as the return value of the loop) collect BODY - evaluate BODY (and collect the return values of the BODY from each iteration into a list, which is returned by the loop) nconc BODY - evaluate BODY (and concatenate the return values using NCONC -- this will cause an error if any return value is not a list!) (format STREAM FMT-STRING ARG1 ...) Print arguments with formatting (use STREAM = T for standard output; STREAM = NIL to return a generated string from the format statement for use in your program) DEBUGGING COMMANDS: :h Print a list of available commands :a Abort to the next debug level :q Abort to the top level interpreter :e Print the last error message :i Inspect the last error message (can get to local variables etc.) :w Print the current stack frame :u Go up one stack frame (i.e., to the caller) :d Go down one stack frame (i.e., to the callee) :t Go to the top frame (top-level expression being evaluated) :b Go to the bottom (most recent) frame :bt List the stack from bottom to top :bt1 - :bt5 List the stack with varying levels of detail :fl Set the frame limit (how many frames to print in backtrace) :rt Return from the current frame (prompts for return values) :c Continue evaluation (if at a continuable error) :rd Re-evaluate the current stack frame STEPPING COMMANDS: (step EXPR) Single-step evaluation of EXPR :s Evaluate the current form in single step mode :n Step over a form (evaluate one form in batch mode) :o Step over a level (evaluate the rest of the current form up to the next return) :c Turn off single step mode and continue evaluation OTHER DEBUGGING FACILITIES: (inspect EXPR) Inspect an arbitrary list object (can "dive in" to examine components of a structured object) (trace FN-NAME) Trace a function (print information every time the function is invoked or returns) (untrace FN-NAME) Untrace a function (untrace) Untrace all functions (break) Go to a break level (continue by typing :c) (break FORMAT-STRING ARGS) Go to a break level and print a formatted message