Title: It is suggested that you use the Lisp interpreter available on the general machine general'asu'edu'
1Getting Started with Lisp
It is suggested that you use the Lisp interpreter
available on the general machine
(general.asu.edu). You are welcome to use other
interpreters while developing, but your programs
should run on the general for grading purposes.
If you absolutely must use a different version
and find that your version is not compatible with
the version on the general, contact the TA before
you turn in any work and make alternate
arrangements.
2Lisp on the General
- Allegro Common Lisp 4.3.1 is available on the
general. The general is the general purpose
Unix account all ASU students have access to. You
can access it with telnet or (better) ssh, at
general.asu.edu. Once you logon to the general,
you will get a Unix prompt. At the prompt, type
in the command cl to start Common Lisp.
3The Lisp Prompt
- Lisp is an interactive, interpreted language. You
can interact with it by entering commands at its
command prompt, or by telling it to read in a
file of commands. Every command entered, either
at the prompt or read from a file, is immediately
evaluated and executed by the interpreter.
4Basic Commands
- (exit) or exit - Exits the Lisp interpreter
- (load file) Loads and evaluates a file.
- CtrlC Breaks execution if you get stuck.
- help Shows list of interpreter commands
- reset Starts the interpreter over
- pop Goes up one level of error breaks
5Manipulating Lists
- Using Quote-
- e.g. USER(1) (1 2 3)
- (1 2 3)
- Using CAR-
- e.g. USER(2) CAR( 1 2 3 4)
- 1
- Using CDR-
- e.g.- USER(3) CDR( 1 2 3)
- ( 2 3)
6Using Lists II
- Cons (cons a b) Creates a new list with a
head of a and a tail of b - e.g. (cons a (b c)) gives (a b c)
- Append (append a b ) Creates a new list out
of a set of lists - e.g. (append '(a b c) '() '(d e f)) gives (a b c
d e f)
7RECURSION
LISP supports recursion just like any procedural
language(eg C) E.g. Raise X to the Nth power
(defun raise (x n) (if ( n 0)
1 ( x (
raise x (- n 1) ) ) ) )
8ITERATION
Using dolist - dolist repeats a set of
operations for as many times as there are entries
in a list. E.g.-USER(1) (dolist (x (a b c))
(print x)) A B C NIL
9Using LOOP E.g.- USER(15)- (loop for x from
1 to 7 collect x) (1 2 3 4 5 6
7)
10Conditional Expressions- Using CASE. E.g.-
User(10)(defun goose (x)
(case x (1 one) (2 two) (3
three) (4 four) (otherwise
many))) goose
11Using dotimes - dotimes repeats an evaluation as
many times as it is requested E.g.-
USER(10)-(dotimes (i 5 i) (print i) o
1 2 3 4
5
12Defining functions using defun
USER(1)(defun funct1 (x) ( x
1)) FUNCT1 USER(2)(funct1(3)) 4
13Input and Output
gt (progn (format t Please enter your
name ) (read-line)) gt (prin1 hello) gt
(format nil Dear A, Our records indicate
Mr. Malatesta) Note There
are many variants. You need to refer to a CL book.
14Other Resources
- http//grimpeur.tamu.edu/colin/lp/ is a Lisp
primer. The Programming Techniques section
should be checked out. - http//www.math.uio.no/cltl/cltl2.html has the
complete text of the 2nd edition of Common Lisp
the Language.