Constructs for Combining Conditions
This section describes constructs that are often used together with if
and cond
to express complicated conditions. The constructs and
and or
can also be used individually as kinds of multiple conditional constructs.
- Function: not condition
This function tests for the falsehood of condition. It returns
t
if condition isnil
, andnil
otherwise. The functionnot
is identical tonull
, and we recommend using the namenull
if you are testing for an empty list.
- Special Form: and conditions…
-
The
and
special form tests whether all the conditions are true. It works by evaluating the conditions one by one in the order written.If any of the conditions evaluates to
nil
, then the result of theand
must benil
regardless of the remaining conditions; soand
returnsnil
right away, ignoring the remaining conditions.If all the conditions turn out non-
nil
, then the value of the last of them becomes the value of theand
form. Just(and)
, with no conditions, returnst
, appropriate because all the conditions turned out non-nil
. (Think about it; which one did not?)Here is an example. The first condition returns the integer 1, which is not
nil
. Similarly, the second condition returns the integer 2, which is notnil
. The third condition isnil
, so the remaining condition is never evaluated.(and (print 1) (print 2) nil (print 3)) -| 1 -| 2 ⇒ nil
Here is a more realistic example of using
and
:(if (and (consp foo) (eq (car foo) 'x)) (message "foo is a list starting with x"))
Note that
(car foo)
is not executed if(consp foo)
returnsnil
, thus avoiding an error.and
expressions can also be written using eitherif
orcond
. Here’s how:(and arg1 arg2 arg3) ≡ (if arg1 (if arg2 arg3)) ≡ (cond (arg1 (cond (arg2 arg3))))
- Special Form: or conditions…
-
The
or
special form tests whether at least one of the conditions is true. It works by evaluating all the conditions one by one in the order written.If any of the conditions evaluates to a non-
nil
value, then the result of theor
must be non-nil
; soor
returns right away, ignoring the remaining conditions. The value it returns is the non-nil
value of the condition just evaluated.If all the conditions turn out
nil
, then theor
expression returnsnil
. Just(or)
, with no conditions, returnsnil
, appropriate because all the conditions turned outnil
. (Think about it; which one did not?)For example, this expression tests whether
x
is eithernil
or the integer zero:(or (eq x nil) (eq x 0))
Like the
and
construct,or
can be written in terms ofcond
. For example:(or arg1 arg2 arg3) ≡ (cond (arg1) (arg2) (arg3))
You could almost write
or
in terms ofif
, but not quite:(if arg1 arg1 (if arg2 arg2 arg3))
This is not completely equivalent because it can evaluate arg1 or arg2 twice. By contrast,
(or arg1 arg2 arg3)
never evaluates any argument more than once.
- Function: xor condition1 condition2
-
This function returns the boolean exclusive-or of condition1 and condition2. That is,
xor
returnsnil
if either both arguments arenil
, or both are non-nil
. Otherwise, it returns the value of that argument which is non-nil
.Note that in contrast to
or
, both arguments are always evaluated.
Copyright © 1990-1996, 1998-2021 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Combining-Conditions.html