Some Basic Lisp
Functions, Special Forms
List builders
cons
(cons <element> <list>)
Constructs a new list by placing <element>
at the head of <list>, returning
a pointer to that new list. Note: cons does not alter the original list!
(cons 'e '(a b c)) ==> (e a b c)
(cons '(a b) '(a b)) ==> ((a b) a b)
append
(append <list1> <list2>)
Constructs a new list by placing the
elements of <list1> in front of the
elements of <list2>. Note: append does not alter
the original list!
(append '(a b) '(c d)) ==> (a b c
d)
(append '(a b) '(a b)) ==> (a b a
b)
list
(list <elt1> <elt2> ...
<eltn>)
Constructs a new list by placing elements
1 through n into a list.
(list 'a 'b 'c) ==> (a b c)
(list '(a b) '(a b)) ==> ((a b)(a b))
Retrievers of list elements
first, car
(first <list>)
Returns the head of <list>.
(first '(a b c)) ==> a
(car '(a b c)) ==> a
(first '((a b)(c d)) ==> (a b)
(car '((a b)(c d)) ==> (a b)
rest,
cdr
(rest <list>)
Returns the tail of <list>.
(rest '(a b c)) ==> (b c)
(cdr '(a b c)) ==> (b c)
(rest '((a b)(c d)) ==> ((d c))
(cdr '((a b)(c d)) ==> ((d c))
nth
(nth <number> <list>)
Returns the <number>th
element of <list>. Beware! Numbering starts
at 0.
(nth 0 '(a b c)) ==> a
(nth 3 '(a b c d e f)) ==> c
first, second, etc.
How many of these are available depends
on the implementation.
Note: You must spell out the word. If you want the more convenient
shorter
forms (i.e., 1st, 2nd, etc.), you must define them yourself.
(second '(a b c)) ==> b
(third '(a b)) ==> NIL
Boolean functions
null
(null <lst>)
Returns T if <list> is empty, NIL
otherwise.
(null '()) ==> T
(null '(a)) ==> NIL
(null nil) ==> T
(null '(nil)) ==> NIL
(null '(())) ==> NIL
member
(member <element>
<list>)
Returns the rest of <list>, starting with <element>, if <element>
is found in
<list>. Note: the default test is eq.
(member 'e '(a b c)) ==> NIL
(member 'e '(a b c) :test ‘equal)
==> NIL
(member 'c '(a b c d)) ==> (c d)
(member '(a b) '((a b) c d)) ==>
NIL
(member '(a b) '(a (a b) c (c d))
:test ‘equal) ==> ((A B) C (C D))
atom
(atom <arg>)
Returns T if <arg>is a Lisp atom, NIL
otherwise.
(setf a '(e f g))
(setf b 'a)
(atom 'a) ==> T
(atom a) ==> NIL
(atom 'b) ==> T
(atom b) ==> T
(atom nil) ==> T
(atom '()) ==> T
listp
(listp <arg>)
Returns T if <arg> is a Lisp list, NIL
otherwise.
(setf a '(e f g))
(setf b 'a)
(listp 'a) ==> NIL
(listp a) ==> T
(listp 'b) ==> NIL
(listp b) ==> NIL
(listp nil) ==> T
(listp '()) ==> T
Notice: nil is both a list and an atom!
zerop
(lzerop <arg>)
Returns T if <arg> has the value 0, NIL
otherwise. Note: <arg>
must be
numeric.
(zerop
0) ==> T
(zerop
1) ==> NIL
(zerop
'a) ==> Error: `A' is not of the
expected type `NUMBER'
Conditional forms
if
(if <test> <thendo>
<elsedo>)
If <test> evaluates to non-nil, then returns the result of
evaluating <thendo>
otherwise returns the result of
evaluating <elsedo>.
(if <test> <thendo>)
If <test> evaluates to non-nil, then returns the result of
evaluating <thendo>
otherwise returns NIL.
(if 1 2 3) ==> 2
(if nil 2 3) ==> 3
(if nil 2) ==> NIL
(if (= 2 3) 2 3) ==> 3
(if (= 2 3)(= 2 2)(= 3 4)) ==> NIL
(if (= 2 3)(= 3 4)(= 2 2)) ==> T
(if (= 2 2)(= 2 2)(= 3 4)) ==> T
(if (= 2 2)(= 3 4)(= 2 2)) ==> NIL
cond
(cond (<test1>
<expressions1>)
(<test2> <expressions2>)
.
.
(<testn>
<expressionsn>))
If <test-i>evaluates to non-nil then returns the result of
evaluating
<expressions-i>. If all tests fail, then returns NIL.
Note: If several expressions
occur in <expressions-i>, the value returned
is the result of evaluating the last of
these.
(cond (1 2)(3 4)) ==> 2
(cond (nil 1)(nil 2)(t 3)) ==> 3
(cond (1 2 3 4)(5 6)) ==> 4
(cond ((= 1 1)(print 1)(print 2)(print
3))
((=
2 2)(print 4))
(t
(print 5))) ==> 1<lf> 2<lf> 3<lf> 3
(cond ((= 1 2)(print 1)(print 2)(print
3))
((=
2 3)(print 4))
(t
(print 5))) ==> 5<lf> 5
(cond ((= 1 2)(print 1)(print 2)(print
3))
((=
2 3)(print 4))
((=
3 4)(print 5))) ==> NIL
Logical operators
or
(or <expr1> <expr2> ...
<exprn>)
Evaluates each expression in turn until
one evaluates to non-nil or
until all have been evaluated. Returns value of last expression
evaluated. Note: This is short-circuit
evaluation, as in C.
(or 1 2 3) ==> 1
(or nil nil 3) ==> 3
(or (= 2 3)(= 2 2)) ==> T
(or (print 1)(print 2)(print 3)) ==>
1<lf> 1
and
(and <expr1> <expr2> ...
<exprn>)
Evaluates each expression in turn until
one evaluates to NIL or
until all have been evaluated. Returns value of last expression
evaluated. Note: Again, short-circuit
evaluation.
(and 1 2 3) ==> 3
(and 1 nil 3) ==> NIL
(and (= 2 2)(= 2 3)) ==> NIL
(and (print 1)(print 2)(print 3)) ==>
1<lf> 2<lf> 3<lf> 3
Arithmetic operators
+ - * / 1+
1-
Comparators
> < >=
<= = eq
equal
(< 1 2) ==> T
(<
2 1) ==> nil
(= 1 1) ==> t
(= 'a 'a) ==> ERROR
Note: = is used only for numeric
values; when eq is applied to lists
the pointers to the lists are checked; equal
is more general
than eq.
(eq 'a 'a) ==> T
(eq '(a) '(a)) ==> NIL
(setf x '(a b))
(setf y x)
(setf z '(a b))
(eq x y) ==> T
(eq x z) ==> NIL
(eq x '(a b)) ==> NIL
(equal x y) ==> T
(equal x z) ==> T
(equal x '(a b)) ==> T
Iteration
do do*
dolist dotimes mapc
mapcar
See separate handout.
Miscellaneous
length prog1 progn
let let* setq
setf eval print
princ terpri :test
(let ((<vbl1> <value1>)
(<vbl2> <value2>) ... (<vbln> <valuen>))
<expressions>)
let binds variables 1 through n
to the values given, then executes
<expressions>, returning the value of the
last expression evaluated.
let* does the same, except that
the bindings proceed serially rather
than in parallel. let & let* are the preferred way to
define local variables.
(let ((x 1)(y 2))(+ x y)(* x y)) ==> 2
(let ((x 1)(y (1+ x)))(+ x y)(* x y))
==> ERROR
(let* ((x 1)(y (1+ x)))(+ x y)(* x y))
==> 2
(let ((x (let* ((x 5)(y (* x x)))(+ x
y)(* x y)(1+ x)))) x) ==> 6
(setf <vbl> <value>)
Binds <vbl> to <value>, returning <value>.
(setf x 5) ==> 5
x ==> 5
(setf x (* 11 7)) ==> 77
x ==> 77
(print (setf x 5)) ==> 5<lf> 5
x ==> 5
(setf x (print 5)) ==> 5<lf> 5
x ==> 5