Lisp AI
Lisp AI
Lisp AI
These notes review the basic ideas of symbolic computation and functional programming as embodied in LISP.
We will cover the basic data structures (s-expressions); the evaluation of functional expressions; recursion as the
expression of repetition; binding and equality; user-defined data structures (defstructs). With minor differences
which will be pointed out, LISP and Scheme (which you should recall from MCS-177 and 178) are very similar.
These notes intended mainly as a refresher for students who have seen Scheme a while back. For a thorough
introduction and a complete reference, I strongly recommend Paul Graham’s ANSI Common Lisp; a copy is in the
lab.
Characteristics of LISP
The main characteristic of LISP is its capability for symbolic computation. Symbols (atoms) are the principal data
type. The operations that can be performed on symbols include equality testing and building up symbol structures.
Putting two symbols together creates a structure, which can then be accessed and taken apart.
Typical applications:
• Language processing, using words as symbols; lists for sentences, trees for grammatical structure.
• Mathematics, involving expressions and equations; trees for expressions.
• Manipulating programs — these are just pieces of (formal) language.
LISP programs are just symbol structures, so LISP programs can modify or create LISP programs. Hence
programs can implement the results of learning by writing new code for themselves; it is also easy to write interpreters
for new languages in LISP.
LISP is a functional language: compute by evaluating nested functional expressions. (Pure) programs have no
side-effects, so they are modular. Simple semantics (i.e., it’s easy to tell what a program does) allows for powerful
program development environments.
Atoms can have a value associated with them, e.g. X might have the value 5.
Numbers (which are also atoms) evaluate to themselves.
Functional expressions are delimited by matching parens: (fn arg1 . . . argn) applies fn to the arguments as follows:
1. Evaluate each argument in turn, then
Sometimes we want to pass an argument directly, without evaluation. To do this we need an identity function,
which doesn’t evaluate ITS argument. QUOTE serves this purpose.
• (QUOTE A) or ’A evalues to A
(+ 2 2) is just a piece of list structure. The next section discusses how to build list structures from atoms.
MCS-385 Notes on Lisp page 2
(defun 1/ (x &optional (checkp nil)) ;comments can follow semicolons like this
(if (and checkp (zerop x))
most-positive-single-float
(/ 1 x)))
Note the use of optional arguments. Here, checkp does not have to be provided in the calling expression, and if
not then it defaults to nil.
“Conditional branching” used in imperative languages is replaced in LISP by conditional evaluation. The if-
expression is evaluated just like any other, but the returned value depends on whether the value of the first argument
to if is nil or not. (Truth values in LISP are nil for false, anything else counts as true. t is used as a readable
default symbol for true. Both t and nil evaluate to themselves, like numbers.)
Complex test expressions can be formed using the functions and or not:
• and returns a non-null value if all its arguments are non-null.
• or returns a non-null value if any of its arguments are non-null.
• not returns a non-null value if its argument is nil.
When you want to return one of several different values depending on several different conditions, use cond (see
example below).
Sometimes, the same expression will be used several times in the same function definition. To simplify the code,
and save time, one should define a temporary variable to stand for the value of the expression:
(defun age-group (person)
(let ((n (age person)))
(cond ((< n 2) ’baby)
((< n 18) ’child)
((< n 120) ’adult)
(t ’dead))))
For temporary variables that are defined at the beginning of the function, as above, one can also use &aux variables
in the parameter list:
MCS-385 Notes on Lisp page 3
Recursion
The simplest way to get repetitive execution in LISP is to use recursion, wherein one uses the function being defined
in the definition of the function itself. The key to thinking clearly about this is the recursion relation that holds for
the problem at hand.
For example:
• The length of a list is one more than the length of its cdr
• The number of atoms in a tree is the sum of the numbers in the left and right-hand sides
• The number of digits in an integer is one more than the number of digits in the integer part of one-tenth of the
integer.
The other thing to take care of is the cases where the recursion relation is false. For example, an empty list
doesn’t have a cdr; a tree that is just an atom doesn’t have left and right hand sides.
(defun count (x) ;; returns number of atoms in list structure x
(if (atom x)
1
(+ (count (car x)) (count (cdr x)))))
Although recursion is often elegant, deeply nested recursion takes a lot of space in some cases, so we also use
mapping and iterative constructs.
Sometimes, one needs to map over a list using a function that doesn’t have a name. For this, and other occasions
demanding dynamically-created functions, we use the special λ-expression:
MCS-385 Notes on Lisp page 4
Warning: if you really want to use a global variable, then for efficiency you should declare it as such using defvar,
instead of just doing a setq to initialize it. This will let the compiler know what kind of thing it is. Also, the standard
is to use asterisks around the name like *this*.
Once again, let me emphasize that a symbol’s value and function can be different. The following expression
evaluates to 6:
(setf f 3)
(setf (symbol-function ’f) #’+) ;; or (defun f (a b) (+ a b))
(f f f)
MCS-385 Notes on Lisp page 5