-*- outline -*- This is a simple tutorial for GNU Common LISP. Launch `clisp' and execute all expression in lines starting with a `>' symbol. If you use this from within EMACS, first do the following: M-: (setq inferior-lisp-program "/net/azdak/dsg/local/i386-unknown-linux/bin/clisp") (i.e. press the Meta and : keys simultaneously, you can use Alt instead of Meta). Then start the Common Lisp interpreter with M-x run-lisp. You can now cut and paste pieces of code from the tutorial to the interpreter and check the results. * LISP ** Examples of Program Structures A few simple example of LISP definitions: > (+ 1 2 3) The arguments to a function call can be arbitrary LISP expression, for example other functions. Thus, LISP supports \emph{higher-order functions}. Function definitions look like this: > (defun sq (x) (* x x)) SQ > (sq 3) 9 Recursion is heavily used in LISP: > (defun fac(n) (if (< n 1) 1 (* n (fac (- n 1))))) FAC > (fac 5) 120 ** Data structures The basic compound data type is a list. The empty list is denoted by () or NIL. The constructor function CONS combines an element and a list into a larger list. The desctructor functions car and cdr (don't ask!) extract the head and the tail of a list. > (defun my-length (l) (if (eq l NIL) 0 (+ 1 (my-length (CDR l))))) MY-LENGTH > (my-length (CONS 1 (CONS 2 NIL))) 2 ** The Art of Quoting LISP programs themselves are lists. Hence, whenever a list is found, the system tries to evaluate it, using the first list element as the function and the rest as its arguments. Therefore, > (my-length (1 2 3 4 5)) *** - EVAL: 1 is not a function name This implicit evaluation can be avoided by quoting a list. For example: > (my-length '(1 2 3 4 5)) 5 ** Lexical vs Dynamic Scoping Dynamic scoping means that a name refers to its most recent definition in the dynamic execution of the program. In Common Lisp, dynamically scoped variables are enclosed by two *'s. Dynamic scoping is useful for global variables, but can be confusing in general since you have to take the dynamic order of evaluation into account in order to decide which variable is in scope. Here is an example of lexical scoping (default in Common LISP): > (setq inc 1) INC > (defun up (x) (+ x inc)) UP > (let ((inc 2)) (up 5)) 6 > (defvar *inc* 1) *INC* > (defun up (x) (+ x *inc*)) > (let ((*inc* 2)) (up 5)) 7 A call to "up" uses the *latest* reference to "inc", not the global! ** Impure Features in LISP LISP contains features for explicit assignment and destructive update. It is therefore ``impure''. The construct setq (for set-quote) is LISP's equivalent to assignment. Common LISP supports data structures such as arrays and records. For example, arrays can be created like this: > (setq a (make-array 3)) #(NIL NIL NIL) > (aref a 1) NIL The setf construct (for set-field) allows to update one field in a data structure: > (setf (aref a 1) 3) 3 > a #(NIL 3 NIL) > (aref a 1) 3 The defstruct construct defines a ``record'' with named fields. > (defstruct foo bar) FOO > (setq a (make-foo)) #s(FOO :BAR NIL) > (foo-bar a) NIL Again, setf can be used to modify the contents of such a structure: > (setf (foo-bar a) 3) 3 > a #s(FOO :BAR 3) > (foo-bar a) 3