University of Toronto
Department of Computer Science

CSC324 - Principles of Programming Languages

Spring 1998
John Mylopoulos

Programming in Lisp: Tutorial Notes I
Prepared by Todd Kelley

Introduction

This handout is meant to be a brief primer for students in csc324 who are
getting started in Lisp programming on the CDF facility. To learn about the
CDF environment itself, you should obtain a copy of "A Student's Guide to
CDF," which is a primer for working on the new CDF workstations.  This
handbook (23 8.5x11" pages) should be available in the University of Toronto
Bookstore.  It gives introductory information about using the new
workstations, using mail and news, working by phone, and other important
issues.

A very important source of information which is often overlooked or forgotten
is the man command.  Knowing how to use the power of the man command can get
you answers to a surprising percentage of the questions you will have
regarding your programming environment.  The man command lets you get the
answers at any time of the day or night.   Try typing "man man" and find out
what "man -k ..." does.

The Course Newsgroup -- cdf.csc324h

A newsgroup has been set up for this course: cdf.csc324h.  It is important for
you to read this newsgroup regularly.  Important (perhaps time-critical)
announcements and pertinent questions and their answers will be posted to this
newsgroup, and you should keep up to date. If you are unfamiliar with with the
newsgroup system and reading articles posted to newsgroups, a fast way for you
to get started is to type rn cdf.csc324h.  Rn is one of the programs used to
read news, and it may be helpful to read its manpage by typing man rn.  It is
a long manpage, but to just get started with rn you will not need to read it
all.

Using Lisp

You are welcome to use any Common Lisp interpreter to do your programming
assignments, not just the one provided on CDF.  Be warned, however, that later
in the course you will need the Common Lisp Object System (CLOS), which is not
part of every Common Lisp interpreter.  This means that whether or not you use
CDF now, you will be using it later.

Starting Lisp

The Lisp used in csc324 is started by the pcl command on CDF. If you type pcl
at the unix prompt, you will see the following:

eddie.cdf% cl
Allegro CL 4.3 [SPARC; R1] (5/10/96 17:51)
Copyright (C) 1985-1996, Franz Inc., Berkeley, CA, USA.  All Rights Reserved.
;; Optimization settings: safety 1, space 1, speed 1, debug 2.
;; For a complete description of all compiler switches given the
;; current optimization settings evaluate (EXPLAIN-COMPILER-SETTINGS).
USER(1):

>

The > character at the end is the Lisp prompt, signifying that it is ready for
you to type in a form.  A form is an s-expression which can be evaluated  to
produce a value.  Lisp is an interpreted language, and it is always in a
read-evaluate-print loop.  This means that it does nothing but read a form,
evaluate it, and print the resulting value.  

Exiting from Lisp

The most common and preferred way to exit from the Lisp interpreter is to type
in one of these forms: (exit), (bye); or, to type the end of input character,
^D (control-D).

>(exit)
Bye.

You may want to exit from the interpreter only temporarily, that is, you want
to suspend the current Lisp process, interact with the shell for a while, then
resume that same Lisp process with all the variable bindings and named
function definitions intact (the Lisp environment unchanged).  For example, if
you are in Lisp and debugging a Lisp program,  you may want to suspend Lisp,
make some changes to the program's file with an editor, then resume the same
Lisp session again.  You would need to do this if you were working at a dumb
terminal rather than at an X console (where you can run Lisp in one window and
edit your Lisp program in another window).  You can suspend the Lisp process
by typing ^Z at the Lisp prompt.  This will stop Lisp and put it in the
background.  You can use the csh command jobs (type man jobs to see what jobs
are stopped or running in the background.  When you want to resume your Lisp
session, type fg %N, where N is the number of the Lisp job, which you can see
when you type jobs.  This will put you back in Lisp, as if you never left.
You can now load your changed program and continue debugging it.

A very important thing to remember is that if you suspend a job with ^Z, you
must remember to resume it (with fg or bg), or kill it (with kill).  It is
very rude to leave unneeded processes (especially processes as huge as Lisp)
in the background!

Now, after all that explanation, there is an easier way to do basically the
same thing. If you want to edit your file from within the Lisp interpreter,
you can type (system "vi myfile.lsp") at the Lisp prompt.

What is a Lisp Program?

Lisp programs are organized as forms and functions.  Typically, when you write
a Lisp program, you will create a file containing a series of forms which will
define named functions as side effects when they are evaluated. Each of these
forms will probably begin with the Lisp macro defun. Then, from the Lisp
interpreter prompt, you will use a builtin Lisp function called load.  Load
accepts a file's name as an argument, and it successively evaluates each form
in the file.  Let's look at an example.  Suppose we want to write a program
that produces a greeting for a person when we supply the person's name.  When
we supply the string "John", the program will produce the string "Hi there,
John".

We begin by defining a named function called \verb+hello+.  To do this, we
create a file named "greeting.lsp" containing this form:

(defun hello (name)
  (concatenate 'string "Hi there, " name))

Concatenate is a built-in Lisp function which concatenates sequences.  In this
case, we are using it to concatenate two sequences of type string. We are now
finished writing our program.  To run our program, we first start Lisp, then,
from the Lisp prompt, we load the program file and run the program.

>(load "greeting.lsp")
Loading greeting.lsp
Finished loading greeting.lsp
T

>(hello "John")
"Hi there, John"

>

Note that our program does not print anything; rather, it produces a
string. The Lisp interpreter evaluates the form (hello "John"), which
evaluates to the string "Hi there, John", and the Lisp interpreter prints this
value.

This is a very short program, and instead of first putting it into a file and
using load to evaluate the form in that file, we we could have typed the form
directly at the Lisp prompt to be evaluated.

>(defun hello (name)
    (concatenate 'string "Hi there, " name))
HELLO

>(hello "Jack")
"Hi there, Jack"

>(exit)
; Exiting Lisp


The Break Loop

If you make a mistake such as trying to evaluate an invalid form, or you type
^C to stop an infinite loop, you will get the break loop prompt. In this case,
you can type :q to get back to the top level prompt.

>(this that)

Error: The function THIS is undefined.
Error signalled by EVAL.
Broken at EVAL.  Type :H for Help.
>>:q

Top level.
>

Storing a Session on a File (by Shanon Xuan Ju)

(dribble pathname) may rebind *standard-input* and *standard-output*,   so as
to send a record of the input/output interaction to a file named   by
pathname. The primary purpose of this is to create a readable  record of an
interactive session. (dribble) terminates the recording of input and output
and closes the  dribble file. 

Here is an example: (notice: I use "script" in unix)

------begin file typescript---------------------------------

Script started on Mon Jan 19 14:58:59 1998
eddie% cl
Allegro CL 4.3 [SPARC; R1] (1/14/97 18:36)
Copyright (C) 1985-1996, Franz Inc., Berkeley, CA, USA.  All Rights Reserved.
;; Optimization settings: safety 1, space 1, speed 1, debug 2.
;; For a complete description of all compiler switches given the
;; current optimization settings evaluate (EXPLAIN-COMPILER-SETTINGS).
USER(1): (dribble "flatten.out")
dribbling to file "flatten.out"
NIL
USER(2): (load "o.lsp")
; Loading ./o.lsp
T
USER(3): (flatten '(a (b)))
(A B)
USER(4): (flatten '(a (b (a a)) (()) ((a))))
(A B A A NIL A)
USER(5): (dribble)
Dribble file flatten.out closed
NIL
USER(6): (exit)
; Exiting Lisp
eddie% ^D  exit

script done on Mon Jan 19 15:00:08 1998

------end of typescript-------------------------------------

Here is the dribble file "flatten.out" (better, I think)

------begin file flatten.out--------------------------------
dribbling to file "flatten.out"
 
NIL
USER(2): (load "flatten.lsp")
; Loading ./flatten.lsp
T
USER(3): (flatten '(a (b)))
(A B)
USER(4): (flatten '(a (b (a a)) (()) ((a))))
(A B A A NIL A)
USER(5): (dribble)

------end of flatten.out------------------------------------

More information about CDF system and regulations (by Thodoros Topaloglou)

| 
|	- type "faq"
|	- type "rules"
|	- read /local/doc/new.cdf.info
| 
| For bugs and problems
| 
|	- first, read /local/doc/known.problems
|	- second, mail admin
| 
| Read newsgroups: ut.cdf.csc324h
| 
| To find about the "traffic" on CDF : type "deadlines"
| 
| 
| Lisp dialects on CDF: 
| 
| - pcl (Austin Kyoto Common Lisp  Version(1.624))
|   (bye)
| 
| - cl (Allegro CL 4.2)			%% more performance (?)
|   (exit)
| 
| 
| (defun hello (name)
|	 (concatenate 'string "Hi there, "name))
| 
| 
| How to get a "free" copy of Lisp for your PC.
| 
|         >> 'clisp' (common lisp) for various platforms (including dos) can
|         >> be ftped from the following:
|         >> ma2s2.mathematik.uni-karlsruhe.de:/pub/lisp/clisp/binaries/dos
|         >> I think it is called clisp.zip.
|