Complex Systems, Spring 1998
Bio 367: Computational Models of Biological Organization
CS 246: Programming Paradigms
Paul Grobstein, Bryn
Mawr College Department of Biology
Clare Congdon, Bryn Mawr
College Computer Science Program
Using clisp on Mainline
The Lisp package we have on Mainline is called
clisp. To run it, just
type clisp at the Unix prompt; to quit, type (exit)
When you're running Lisp, you'll have a new prompt, the angle bracket. Most of
the legitimate things to type should be enclosed in parentheses, such as
(exit). The parentheses are absolutely important. (Some folks will
claim that LISP is an acronym for Lots of Irritating and Silly Parentheses.
But it really stands for LISt Processing language. The parentheses are used to
denote the lists.)
Lisp curiosities
- Lisp is an interpreted language, which means that the code need not be
compiled to be run. Instead, everything that you type at the prompt is
evaluated immediately.
- Most Lisp expressions are lists, meaning that they are enclosed in
parentheses, such as
(+ 3 5)
- Some Lisp expressions are atoms, meaning that they are single symbols not
enclosed in parentheses, such as
3
- Lisp uses what's called prefix notation, which means that the name
of the function or operator (like +) comes first, and the arguments or
operators come afterwards, as in (+ 3 5)
- Lisp expressions can be nested within other Lisp expressions, such as
(* (+ 3 5) (- 10 2))
The expressions are evaluated from left to right and from the inside
out, so the above is equivalent to (* 8 5)
- All Lisp expressions typed at the prompt return a value. The absense of a
return value is NIL, which is comparable to "false" in other
programming languages. T is the return value for "true".
You will see a lot of NIL's when you are typing expressions
at the Lisp prompt.
- Because Lisp is interpreted, if you make an error in what you type --
which, recall, is evaluated immediately -- you will be thrown
immediately into a debugger. This would be handy if we were actually
trying to learn to program in Lisp, but for our uses right now is an
annoyance. To get out, type
abort
- Lisp is an untyped language, which doesn't really need concern us, but I
wanted to mention it for the benefit of the programmers in the class.
The implication of this is that when you define a function (which we
will not do), you don't need to specify the types of the arguments.
- To set the value of a variable, use SETF, as in:
(setf x 3)
(setf y 5)
(+ x y)
Variables will be created on the fly. (They will be globals variables, but
we don't have to worry about that.)
Notice that if you type the above three lines, the first expression will
return 3, the second will return 5, and the third will return 8.
- The comment character in Lisp is a semicolon. (Everything typed after a
semicolon up to the end of the line is ignored.)
The LISP functions we care about for this lab
For this lab, we will only be using logical functions as the primitives for
our genetic programming.
- Although we might think of the inputs (and outputs) to our functions as
0's and 1's, in Lisp, they're more correctly NIL's and T's.
For the purposes of the following examples, lets define a few
variables:
(setf w T)
(setf x T)
(setf y NIL)
(setf z NIL)
- AND is a logical AND; it returns T when both its arguments are T
and NIL otherwise, e.g.
(and w x) --> T
(and w y) --> NIL
(and y z) --> NIL
- OR is a logical OR; it returns T when at least one of its
arguments is T and NIL if both are NIL, e.g.
(or w x) --> T
(or w y) --> T
(or y z) --> NIL
Note that this is not an exclusive or.
- NOT is a logical NOT; it returns T when its single argument
is NIL and vice versa, e.g.
(not w) --> NIL
(not y) --> T
- EQUAL is what you think it is; it returns T when its arguments
are both T and when they are both NIL, e.g.
(equal w x) --> T
(equal w y) --> NIL
(equal y z) --> T
- IF is a conditional statement in Lisp. It takes three
arguments. The first is evaluated; if it returns T, the second
argument is returned, and if the first argument is NIL, the third
argument is returned.
Programmers can think of this as
if (the 1st expression is T)
then (evaluate the 2nd expression)
else (evaluate the 3rd expression)
But you still have to think in Lisp terms with that.
This will take a little while to sink in for all of you, but here's
some examples:
(if x y z) --> NIL (x is T, so y is returned)
(if y x w) --> T (y is NIL, so w is returned)
(if (and w x)
x
z) --> T ((and w x) is T, so x is returned)
The last example starts to get at the flavor of Lisp expressions in general,
which can be "nested" to an arbitrary depth, for example
(and (or y (not z)) (equal (or y z) (and x y)))
The above expression evaluates to T (which you can try out for yourself by
first doing all the SETF's for the variables and then typing the expression at
the Lisp prompt.) From the inside out,
(not z) is T, so (or y (not z)) is T
(or y z) is NIL
(and x y) is NIL
(equal (or y z) (and x y)) is T (both parts are NIL)
both arguments to the AND are T, so the whole expression is T
It may be easier at first to type these things at the Lisp prompt rather than
trying to figure it out in your head, so that you get a feel for the T and NIL
convention.
Maintained by:
Clare Bates Congdon (ccongdon@brynmawr.edu)
Paul Grobstein (pgrobste@brynmawr.edu)