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
tif condition isnil, andnilotherwise. The functionnotis identical tonull, and we recommend using the namenullif you are testing for an empty list.
- Special Form: and conditions…
-
The
andspecial 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 theandmust benilregardless of the remaining conditions; soandreturnsnilright 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 theandform. 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 ⇒ nilHere 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.andexpressions can also be written using eitheriforcond. Here’s how:(and arg1 arg2 arg3) ≡ (if arg1 (if arg2 arg3)) ≡ (cond (arg1 (cond (arg2 arg3))))
- Special Form: or conditions…
-
The
orspecial 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-
nilvalue, then the result of theormust be non-nil; soorreturns right away, ignoring the remaining conditions. The value it returns is the non-nilvalue of the condition just evaluated.If all the conditions turn out
nil, then theorexpression 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
xis eithernilor the integer zero:(or (eq x nil) (eq x 0))
Like the
andconstruct,orcan be written in terms ofcond. For example:(or arg1 arg2 arg3) ≡ (cond (arg1) (arg2) (arg3))You could almost write
orin 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,
xorreturnsnilif 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