As you do this laboratory take note of what you are learning about LISP in another file. (You can open a window to edit this file at the same time as you run LISP in another terminal window, and also browse pages in a third window)
Review, edit, check for speeling mistooks, and publish the result. Check out what it looks like and improve if necessary.
Let me know by calling me across to your workstation when done.
To earn full credit the work must be done before the
end of the lab
and should contain a
list of at least
10 notes. Each note is a short paragraph with one or two
sentences, some LISP expressions, and their values.
LISP Versions and Commands
On the computers in JB359 the command xlisp should
run XLISP for you. If it doesn't use this command
~dick/bin/lispOn other servers use:
/share/bin/xlisp(or add /share/bin to your PATH variable).
On the many workstations and servers the lisp command will probably start up a different version of LISP. The command
(exit)will terminate it.
For beginning LISP, use the longer commands:
xlispor
~dick/bin/lispor
/share/bin/lispto run LISP.
1
123445
-123
3.14159Some LISPs don't have floating point numbers. The last expression/value will test the interpreter for you.
"This is a string"
")This is an ok string!"
":-)"An error will happen with the following:
'This is a string'
TThe other boolean value is NIL.
NILLISP (like C) interprets a NIL as false.
. . . . . . . . . ( end of section Elementary Constants) <<Contents | End>>
Simple Arithmetic expressions
The operation comes first, followed by its arguments,
and the whole thing is put in parentheses:
( + 2 3 )
( * 2 3 )Operators are atoms + - * / rem and other symbols.
(+ 1 2)
(+ 1 2 3 4 5 6 7 8 9)Try a few of your own!
Now try some mistakes so you can see what various messages mean:
1*2
( 2 + 3 )
(*2 3)
()
NILAll inputs except constants are treated as function calls or variables. The "function" "QUOTE" turns lists into constants however. It returns a copy of its argument with NO evaluation or changes. Quote stops the evaluation process.
(quote example)Omit the (QUOTE...) and the data is treated as a function or variable often giving an error:
example
(example)Quoted Lists:
(quote (all input is fed into an interpreter))Quoted lists can contain sublists:
(quote (now is the time (he said) to come to the party))Quoted lists inside quoted lists are ok.
(quote (quote stops the interepreter evaluating a list))
(quote a)The blip '... is a short hand form. Use it in place of (Quote ....)
'example
'(all input is fed into an interpreter)
'(quote stops the interepreter evaluating a list)
'(now is the time (he said) to come to the party)
Here are some quick review questions. Why do the expressions below work the way they do?
'NIL
NIL
'1
1
(+ 1 2)
'(+ 1 2)
'"(quote a)"
"(quote a)"
(quote a)
'a
a
(a)
'(a b c d)
(a b c d)
'(a b (c d))
(a b '(c d))
Try these: some do not always work.... why?
a
(setq a 1234)
a
'a
(quote a)
(* a a)
(a + a)
(setq date '(9 May 2005))
date
(+ 1 2)
(* 3 2)
(- 3 2)
(/ 24 2)LISP thinks that a variable is a function, if it is the first item in the list being evaluated:
a
(a)
(date)
(+ (* 2 3) (- 3 2) )
(+ (/ 1.0 1.0 ) (/ 1.0 2.0) (/ 1.0 3.0) (/ 1.0 4.0) )The innermost are evaluated before the outermost ones, like this:
(trace + * - / )
(+ (* 2 3) (- 3 2) )
(* 2 (+ (* 2 3) 1))
(+ (/ 1.0 1.0 ) (/ 1.0 2.0) (/ 1.0 3.0) (/ 1.0 4.0) )You can turn tracing off like this:
(untrace + * )
(untrace + * - )
(untrace + * - /)
Try enough different LISP expressions for the notation to become a little more natural... and then try to express the following in LISP
Functions of lists
Lists are handled by functions that have one or more lists
as arguments and produce a single list as a result. Here is a
list of the basic functions in Common LISP:
(atom 'a)
(atom '(a b c))
(atom '())
(null '())
(null nil)
(null 'nil)
(null 'a))
(null '(a b))
(cons (quote a) (quote b))
(cons 'a 'b)
(cons 'b 'a)
(cons 'a)
(cons '(a b ))
In C++ we would have
(cons 1 3)
(cdr '(1 . 3))
(cdr '(1 . 3))
(car '(a . b))
(cdr '(a . b))
(cons (car '(a . b)) (cdr '(a . b)))
(car (cons 1 2))
(cdr (cons 1 2))
(list 1)
(list 1 2)
(list 1 2 3)
(1 2 3)
(list 1 2 3 4 5 6 7 8 9 )
(list 'a 'b 'c)
(list 19 'feb 1999)
(list 'CS320 "Programming Languages" 4 )
(setq x '(a . b))
x
'x
(atom x)
(car x)
(cdr x)
car (x)
'(car x)
(cons (car x) (cdr x))
(setq x (+ 1 2))
x
'x
The list function assembles lists for you:
(setq date (list 9 'may 2005))
(setq class (list 'CS320 "Programming Languages" 11 ) )
(list class 'on date )You can extract any item in a list:
date
(car date)
(cadr date)
(caddr date)The function car extracts the head or first item of a pair. The function cdr is the tail of the pair, so cadr is the head of the tail - the second item. cddr is the cdr of the cdr and so the tail of the tail. So caddr is the 3rd item in a list.
Try the following expressions to find out which functions (caddd*r ...) our LISP has, and which it does not have:
(setq x '(mary had a little lambda))
x
(car x)
(cdr x)
(cadr x)
(cddr x)
(caddr x)
(cdddr x)
(cadddr x)
(caddddr x)Most modern versions of LISP have a function nth that extracts one item from a list: (nth 12 x) is the 12th item in x starting with zero. For example, (nth 0 x) gives the same value as (car x).
(nth 0 x)
(nth 1 x)
(nth 2 x)
(nth 4 x)
Here is a very common error made by beginners:
(setq x (mary had a little lamb))Rule 1: the first thing after a parenthesis is an operator. And 'mary' is not an operator!
(cons 1 nil)
(cons 1 (cons 2 nil))
(cons 1 (cons 3 (cons 4 nil)))
(cons 'a nil)
(cons 'a 'b)
(cons 'a (cons 'b nil))
(cons 'a (cons 'b (cons 'c nil)))
(cons 'a (cons 'b (cons 'c (cons d nil ))))
(cons (cons (cons 'a 'b) 'c) 'd)
(cons nil nil)
(setq x '(surprised))
(setq a '(mary had a little lambda))
(setq b '(the doctor was) )Now I combine them into a single list:
(append a b x)
Be careful constructing lists: The following are often written by mistake when (append ...) is needed:
(a b x)(Wrong!)
(quote a b x)(Wrong!)
(cons a b x)(Wrong!)
(list a b x)(Wrong!)
But this works
(append a b x)(Excellent!)
More examples... which is good and which bad?
(setq y (x x)) ; see Rule 1 above
(setq y '(x x)) ; Rule 2: A quoted list can't have any variables
(setq y (CONS x x)) ; Cons only constructs pairs, not lists.
(setq y (LIST x x)) ; Use list to make lists of lists
(setq y (LIST x x x))
(setq y (APPEND x x x)) ; use APPEND to concatenate lists
(COND (T 'a) (NIL 'b))
(COND (NIL 'a) (T 'b))
(COND (NIL 'a) (T 'b) (NIL 'c))
(COND (NIL 'a) (NIL 'b) (T 'c))
A three way comparison:
(setq a 1) (setq b 3)
(COND ((= a b) "a=b") ((< a b) "a<b") (T "a>b"))
(setq b (- b 2))
(COND ((= a b) "a=b") ((< a b) "a<b") (T "a>b"))
(setq a (+ 1 a))
(COND ((= a b) "a=b") ((< a b) "a<b") (T "a>b"))
(if T 'a 'b)
(if NIL 'a 'b)
(if True 'a 'b)
(if (> 2 1) 2 1)
(if (> 1 2) 1 2)
Creating new functions
Next lab!
[ 12.html ]
How To Leave LISP
To exit lisp, input the EOT character CTRL/D
or input the expression (exit).
. . . . . . . . . ( end of section CS320 LISP Laboratory Number 1) <<Contents | End>>
See Also
The WikiWikiWeb has some pages on LISP in Practice:
[ wiki?CommonLisp ]
[ wiki?SchemeLanguage ]
[ wiki?LispLanguage ]
Also see the brilliant EverythingIsa page: [ wiki?EverythingIsa ] on the WikiWikiWeb.
. . . . . . . . . ( end of section CS320 Lab/11 LISP Laboratory Number 1) <<Contents | End>>
Check the Preparation for next class
[ ../12.html ]
End