1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<h3 class="section">Constructs for Combining Conditions</h3> <p>This section describes constructs that are often used together with <code>if</code> and <code>cond</code> to express complicated conditions. The constructs <code>and</code> and <code>or</code> can also be used individually as kinds of multiple conditional constructs. </p> <dl> <dt id="not">Function: <strong>not</strong> <em>condition</em>
</dt> <dd><p>This function tests for the falsehood of <var>condition</var>. It returns <code>t</code> if <var>condition</var> is <code>nil</code>, and <code>nil</code> otherwise. The function <code>not</code> is identical to <code>null</code>, and we recommend using the name <code>null</code> if you are testing for an empty list. </p></dd>
</dl> <dl> <dt id="and">Special Form: <strong>and</strong> <em>conditions…</em>
</dt> <dd>
<p>The <code>and</code> special form tests whether all the <var>conditions</var> are true. It works by evaluating the <var>conditions</var> one by one in the order written. </p> <p>If any of the <var>conditions</var> evaluates to <code>nil</code>, then the result of the <code>and</code> must be <code>nil</code> regardless of the remaining <var>conditions</var>; so <code>and</code> returns <code>nil</code> right away, ignoring the remaining <var>conditions</var>. </p> <p>If all the <var>conditions</var> turn out non-<code>nil</code>, then the value of the last of them becomes the value of the <code>and</code> form. Just <code>(and)</code>, with no <var>conditions</var>, returns <code>t</code>, appropriate because all the <var>conditions</var> turned out non-<code>nil</code>. (Think about it; which one did not?) </p> <p>Here is an example. The first condition returns the integer 1, which is not <code>nil</code>. Similarly, the second condition returns the integer 2, which is not <code>nil</code>. The third condition is <code>nil</code>, so the remaining condition is never evaluated. </p> <div class="example"> <pre class="example">(and (print 1) (print 2) nil (print 3))
-| 1
-| 2
⇒ nil
</pre>
</div> <p>Here is a more realistic example of using <code>and</code>: </p> <div class="example"> <pre class="example">(if (and (consp foo) (eq (car foo) 'x))
(message "foo is a list starting with x"))
</pre>
</div> <p>Note that <code>(car foo)</code> is not executed if <code>(consp foo)</code> returns <code>nil</code>, thus avoiding an error. </p> <p><code>and</code> expressions can also be written using either <code>if</code> or <code>cond</code>. Here’s how: </p> <div class="example"> <pre class="example">(and <var>arg1</var> <var>arg2</var> <var>arg3</var>)
≡
(if <var>arg1</var> (if <var>arg2</var> <var>arg3</var>))
≡
(cond (<var>arg1</var> (cond (<var>arg2</var> <var>arg3</var>))))
</pre>
</div> </dd>
</dl> <dl> <dt id="or">Special Form: <strong>or</strong> <em>conditions…</em>
</dt> <dd>
<p>The <code>or</code> special form tests whether at least one of the <var>conditions</var> is true. It works by evaluating all the <var>conditions</var> one by one in the order written. </p> <p>If any of the <var>conditions</var> evaluates to a non-<code>nil</code> value, then the result of the <code>or</code> must be non-<code>nil</code>; so <code>or</code> returns right away, ignoring the remaining <var>conditions</var>. The value it returns is the non-<code>nil</code> value of the condition just evaluated. </p> <p>If all the <var>conditions</var> turn out <code>nil</code>, then the <code>or</code> expression returns <code>nil</code>. Just <code>(or)</code>, with no <var>conditions</var>, returns <code>nil</code>, appropriate because all the <var>conditions</var> turned out <code>nil</code>. (Think about it; which one did not?) </p> <p>For example, this expression tests whether <code>x</code> is either <code>nil</code> or the integer zero: </p> <div class="example"> <pre class="example">(or (eq x nil) (eq x 0))
</pre>
</div> <p>Like the <code>and</code> construct, <code>or</code> can be written in terms of <code>cond</code>. For example: </p> <div class="example"> <pre class="example">(or <var>arg1</var> <var>arg2</var> <var>arg3</var>)
≡
(cond (<var>arg1</var>)
(<var>arg2</var>)
(<var>arg3</var>))
</pre>
</div> <p>You could almost write <code>or</code> in terms of <code>if</code>, but not quite: </p> <div class="example"> <pre class="example">(if <var>arg1</var> <var>arg1</var>
(if <var>arg2</var> <var>arg2</var>
<var>arg3</var>))
</pre>
</div> <p>This is not completely equivalent because it can evaluate <var>arg1</var> or <var>arg2</var> twice. By contrast, <code>(or <var>arg1</var> <var>arg2</var>
<var>arg3</var>)</code> never evaluates any argument more than once. </p>
</dd>
</dl> <dl> <dt id="xor">Function: <strong>xor</strong> <em>condition1 condition2</em>
</dt> <dd>
<p>This function returns the boolean exclusive-or of <var>condition1</var> and <var>condition2</var>. That is, <code>xor</code> returns <code>nil</code> if either both arguments are <code>nil</code>, or both are non-<code>nil</code>. Otherwise, it returns the value of that argument which is non-<code>nil</code>. </p> <p>Note that in contrast to <code>or</code>, both arguments are always evaluated. </p>
</dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Combining-Conditions.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Combining-Conditions.html</a>
</p>
</div>
|