CM10228 / Programming Ib:
This tutorial is designed to give you a feel for
programming in a very different language than Java or C. The
intention of this tutorial is to help develop your understanding of
what are general considerations in programming, and what are
language-specific issues. In this tutorial you will learn how
run queries and define functions in an interpreted language which is
not strictly typed. You will also get to manipulate a simple
structure, the list.
This tutorial must be run from one of the BUCS unix computers.
The language you are using is called Lisp, but from BUCS it is
"euscheme". (Scheme is actually a dialect of Lisp, but
differences aren't important for us here.) Here are some Notes
using Euscheme written by Dr. Bradford, but you probably won't
them for this tutorial.
If you know emacs, you may find it easier to run this program from
inside a shell window (Meta-X shell) so that you can have access to
editing commands. If you don't know emacs, just try to type
correctly from the command line.
If you get interested in Lisp and have your own computer, you may
to download a free Lisp IDE LispWorks from http://www.lispworks.com/.
runs on MS Windows, Mac OS X or Linux. Notice though that
it's a real lisp so will work a little different than the below
instructions. You can also download an entire book about lisp for
Getting to know the interpreter
If a language is interpreted, then you can run any command in it
command prompt. This allows you not only to run functions, but
also to look at and change values in its memory. Lisp has an
object system, but unlike Java doesn't force you to use it.
this is just a quick tutorial, we won't bother with the object
- To begin with, type an integer (for example, 5) and see what
This is your first interaction with the interpreter. You have
asked it the value of a basic type, and it has told you the answer.
- Now try typing a character or word. (A character will be
interpreted as a short word.)
Words are names for memory locations, and odds are you have chosen a
word that hasn't already been defined, so you are now in the
debugger. Have a look at the options you have by typing help:,
but probably what you want to type now is top: to get back to the
Now try typing the same word with a single quote in front of it, for
example (assuming > is the prompt)
A quote refers to the token
can think of the token as the name for a piece of memory, not the
Here is how you set a value in Lisp:
- Try setting your word to an integer value.
- Now what happens when you type it without a quote?
Getting to know Lisp syntax
Lisp statements are surrounded by parentheses. They are in
notation, which means that the first word is always a verb of some
type, and the rest of the words are arguments. #'setq, which
used above, takes even numbers of arguments. The first, third,
fifth... arguments are names of locations, the second, fourth,
arguments are values.
There are still types in lisp, & the operators know what types
things are. So (/ 3 5) is different from (/ 3.0 5.0).
- Try adding 2 and 2. The symbol for addition in Lisp is
like in Java.
- Does / work like it does in Java?
happens when you use / for integers? for reals? for
That variable has now taken on the type of its
contents. Lisp evaluates all of the arguments of a statement
before it trys to execute a statement.
- Try adding 2, 3 and 5 in a single statement.
adding something to the word you've set to a value using setq
The basic conditional "if" in lisp looks like this (if [test] [then] [else]),
where square brackets mean you can put an expression there (the
is optional.) For example, try typing:
Lisp programmers often use a more general form of conditional called
which lets you have lots of conditionals before the else case, and
lets you have as many statements as you want within each case.
takes a list of lists as arguments, the first element of each list
the conditional, and they will each be tested in order.)
((= 5 (+ 2 2)) (print "I can do math."))
((= 6 (+ 3 3)) (print "I can do math second shot.") (print "I don't feel so bad."))
(#t (print "looks like I can't do math at all."))
- Try playing with the integer values in these examples so that
see how to reach each of the cases. Notice what the return
is for the cond,
especially for the case that two statements got
Defining a function in Lisp
Here's a very simple function definition in lisp:
(defun add-5 (xxx)
(+ 5 xxx))
- What does the interpreter return to you when you have typed
- What happens now if you type in the word add-5 to the
A defun statement takes a name, a list of variable names, and then
number of statements which are the code that gets run for that
function. It returns the value of the last statement that
You can see that since you aren't using a
compiler or a type system, you can get yourself in trouble!
- Try typing (add-5 12). What happens?
about (add-5 'myword)?
Lists in Lisp
Lists are the most basic data structure in lisp. Even
are really lists. Lists are also in parentheses. To
them, you also have to put a single quote in front of them, e.g '(1
3), otherwise the interpreter will try to evaluate them, like it
statement. Lists are such an important concept in lisp, the
list has a special name, nil. In lisp, the value of "nil" and
are exactly the same, which you can test like this:
(eq nil '())
Nil is also the
of #t, that is, it is also the boolean value "false". That's
important lists are to lisp!
Unfortunately, euscheme is a rather archaic version of lisp, so you
can't use the contemporary commands "first" and "rest" to take apart
list as you were taught in lecture. Instead of "first" you
to use "car", and instead of "rest" you have to use "cdr".
probably good to learn the old historic names for these functions in
case you ever run into them, but it will make the below code look
comprehensible. Recall also that you build a list by using
"cons". Try running the following code:
(setq shortlist (cons 1 nil))
(setq medlist (cons 1 (cons 2 nil)))
(setq longlist '(1 2 3 4))
To test for whether a list is empty, use the predicate "null" like
(null (cdr shortlist))
(null (cdr longlist))
Processing lists in Lisp
One of the basic design patterns often used in Lisp is to process a
list using recursion. In this section I will show you an
instance of that pattern, and then ask you to write a couple
functions. When you are through with those functions, you are
through with this tutorial!
Here's a function that just prints a list out item by item.
put an error check in it just to show you that such a thing is
(cerror "Argument to silly-print-list was
a list!" <bad-type>))
(print "all done!"))
(print (car mylist))
(silly-print-list (cdr mylist))
); end of #t option
)) ; end of
cond and defun
(silly-print-list '(my dog has fleas))
Now, here are some functions to try to write:
- Write a simple function called "add-two" that takes two
arguments, both numbers and sums them.
- Write a function called "sum list" which uses add-two and
recursion to sum all the numbers in a list. (Obviously you
just do this with +, but the point is to practice recursion!)
- Write a function called "reverse" that builds up a list
"cons"!) that is the reverse of its input list. So
(reverse '(a b
c)) returns '(c b a).
- Extra bonus question --- can you write a function
which reverses lists inside
of lists? E.g. '(1 2 3 (4 5) (6 7 8))
page author: Joanna
last updated: 6 Feb 2013