Lisp provides a number of conditional constructs, the most complex of
cond) takes a list of conditions, the first of which
evaluates to true has its
associated list of forms evaluated. Theoretically this is the only
conditional special form necessary—all others can be implemented as
if form is the nearest thing in Lisp to the
if-then-else construct found in most programming languages.
First the condition form is evaluated, if it returns true the true-form is evaluated and its result returned. Otherwise the result of an implicit progn on the else-forms is returned. If there are no else-forms false is returned.
Note that one of the true-form or the else-forms is completely ignored—it is not evaluated.
(if (special-form-p if) "`if' is a special form" "`if' is not a special form") ⇒ "`if' is not a special form"
condition is evaluated, if it is true the result of an implicit progn on the true-forms is returned, otherwise false is returned.
(when t (message "Pointless") 'foo) ⇒ foo
This special form evaluates condition, if its computed value is
() is returned. Otherwise the else-forms are
evaluated sequentially, the value of the last is returned.
cond special form is used to choose between an arbitrary
number of conditions. Each clause is a list; the car of which is
a condition, the cdr is a list of forms to evaluate (in an
progn) if the condition evaluates to true.
This means that each clause looks something like:
and a whole
cond form looks like:
(cond (condition-1 body-forms-1…) (condition-2 body-forms-2…) …)
The condition in each clause is evaluated in sequence
(condition-1, then condition-2, …), the first one
which evaluates to a true value has an implicit
performed on its body-forms. The value of this
also the value of the
If the true condition has no body-forms the value returned
is the value of the condition. If none of the clauses has a
true condition the value of the
Often you want a default clause which has its body-forms
evaluated when none of the other clauses are true. The way to do this
is to add a clause with a condition of
body-forms of whatever you want the default action to be.
(cond ((stringp buffer-list)) ;Clause with no body-forms ((consp buffer-list) (setq x buffer-list) ;Two body-forms t) (t ;Default clause (error "`buffer-list' is corrupted!"))) ⇒ t
This special form is similar to
cond, but switches on the result
of evaluating a single form key, checking for equality with a
number of other values, defined by the clauses. If any of these
other values is the same as the result of evaluating key, then a
sequence of forms associated with the value is evaluated.
Each element of the clauses list has the format:
((value-1 value-2 … value-n) forms…)
Each of the values in the car of the clause is tested for equality with
key, using the
eql function. If any test positively, then
the associated forms are evaluated and the resulting value
becomes the result of the special form.
Instead of supplying a list of possible values, it is also possible to
just specify the symbol
t. If such a clause is encountered, and
no other clauses have matched the value of key, then this clause
is assumed to match by default.
If any of the values in the clauses appear multiply, then the behaviour of the construct is undefined.
Here is an example use of
(case foo ((bar baz) (print "It was either bar or baz")) ((quux) (print "It was quux")) (t (print "I've no idea what it was...")))
There are also a number of special forms which combine conditions together by the normal logical rules.
The first of the forms is evaluated, if it is true its
value is the value of the
or form and no more of
are evaluated. Otherwise this step is repeated for the next member of
If all of the forms have been evaluated and none have a
true value the
or form evaluates to false.
(or nil 1 nil (beep)) ;
(beep)won't be evaluated ⇒ 1
The first of the forms is evaluated. If it is false no more
of the forms are evaluated and false is the value of
and statement. Otherwise the next member of forms is
evaluated and its value tested. If none of the forms are
false the computed value of the last member of forms is
returned from the
(and 1 2 nil (beep)) ;
(beep)won't be evaluated ⇒ () (and 1 2 3) ;All forms are evaluated ⇒ 3
This function inverts the truth value of its argument. If object is true, false is returned, otherwise true is returned.
(not nil) ⇒ t (not t) ⇒ () (not (not 42)) ⇒ t