diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/conditionals.html | |
new repository
Diffstat (limited to 'devdocs/elisp/conditionals.html')
| -rw-r--r-- | devdocs/elisp/conditionals.html | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/devdocs/elisp/conditionals.html b/devdocs/elisp/conditionals.html new file mode 100644 index 00000000..c0c4d3e5 --- /dev/null +++ b/devdocs/elisp/conditionals.html @@ -0,0 +1,54 @@ + <h3 class="section">Conditionals</h3> <p>Conditional control structures choose among alternatives. Emacs Lisp has five conditional forms: <code>if</code>, which is much the same as in other languages; <code>when</code> and <code>unless</code>, which are variants of <code>if</code>; <code>cond</code>, which is a generalized case statement; and <code>pcase</code>, which is a generalization of <code>cond</code> (see <a href="pattern_002dmatching-conditional">Pattern-Matching Conditional</a>). </p> <dl> <dt id="if">Special Form: <strong>if</strong> <em>condition then-form else-forms…</em> +</dt> <dd> +<p><code>if</code> chooses between the <var>then-form</var> and the <var>else-forms</var> based on the value of <var>condition</var>. If the evaluated <var>condition</var> is non-<code>nil</code>, <var>then-form</var> is evaluated and the result returned. Otherwise, the <var>else-forms</var> are evaluated in textual order, and the value of the last one is returned. (The <var>else</var> part of <code>if</code> is an example of an implicit <code>progn</code>. See <a href="sequencing">Sequencing</a>.) </p> <p>If <var>condition</var> has the value <code>nil</code>, and no <var>else-forms</var> are given, <code>if</code> returns <code>nil</code>. </p> <p><code>if</code> is a special form because the branch that is not selected is never evaluated—it is ignored. Thus, in this example, <code>true</code> is not printed because <code>print</code> is never called: </p> <div class="example"> <pre class="example">(if nil + (print 'true) + 'very-false) +⇒ very-false +</pre> +</div> </dd> +</dl> <dl> <dt id="when">Macro: <strong>when</strong> <em>condition then-forms…</em> +</dt> <dd> +<p>This is a variant of <code>if</code> where there are no <var>else-forms</var>, and possibly several <var>then-forms</var>. In particular, </p> <div class="example"> <pre class="example">(when <var>condition</var> <var>a</var> <var>b</var> <var>c</var>) +</pre> +</div> <p>is entirely equivalent to </p> <div class="example"> <pre class="example">(if <var>condition</var> (progn <var>a</var> <var>b</var> <var>c</var>) nil) +</pre> +</div> </dd> +</dl> <dl> <dt id="unless">Macro: <strong>unless</strong> <em>condition forms…</em> +</dt> <dd> +<p>This is a variant of <code>if</code> where there is no <var>then-form</var>: </p> <div class="example"> <pre class="example">(unless <var>condition</var> <var>a</var> <var>b</var> <var>c</var>) +</pre> +</div> <p>is entirely equivalent to </p> <div class="example"> <pre class="example">(if <var>condition</var> nil + <var>a</var> <var>b</var> <var>c</var>) +</pre> +</div> </dd> +</dl> <dl> <dt id="cond">Special Form: <strong>cond</strong> <em>clause…</em> +</dt> <dd> +<p><code>cond</code> chooses among an arbitrary number of alternatives. Each <var>clause</var> in the <code>cond</code> must be a list. The <small>CAR</small> of this list is the <var>condition</var>; the remaining elements, if any, the <var>body-forms</var>. Thus, a clause looks like this: </p> <div class="example"> <pre class="example">(<var>condition</var> <var>body-forms</var>…) +</pre> +</div> <p><code>cond</code> tries the clauses in textual order, by evaluating the <var>condition</var> of each clause. If the value of <var>condition</var> is non-<code>nil</code>, the clause succeeds; then <code>cond</code> evaluates its <var>body-forms</var>, and returns the value of the last of <var>body-forms</var>. Any remaining clauses are ignored. </p> <p>If the value of <var>condition</var> is <code>nil</code>, the clause fails, so the <code>cond</code> moves on to the following clause, trying its <var>condition</var>. </p> <p>A clause may also look like this: </p> <div class="example"> <pre class="example">(<var>condition</var>) +</pre> +</div> <p>Then, if <var>condition</var> is non-<code>nil</code> when tested, the <code>cond</code> form returns the value of <var>condition</var>. </p> <p>If every <var>condition</var> evaluates to <code>nil</code>, so that every clause fails, <code>cond</code> returns <code>nil</code>. </p> <p>The following example has four clauses, which test for the cases where the value of <code>x</code> is a number, string, buffer and symbol, respectively: </p> <div class="example"> <pre class="example">(cond ((numberp x) x) + ((stringp x) x) + ((bufferp x) + (setq temporary-hack x) ; <span class="roman">multiple body-forms</span> + (buffer-name x)) ; <span class="roman">in one clause</span> + ((symbolp x) (symbol-value x))) +</pre> +</div> <p>Often we want to execute the last clause whenever none of the previous clauses was successful. To do this, we use <code>t</code> as the <var>condition</var> of the last clause, like this: <code>(t +<var>body-forms</var>)</code>. The form <code>t</code> evaluates to <code>t</code>, which is never <code>nil</code>, so this clause never fails, provided the <code>cond</code> gets to it at all. For example: </p> <div class="example"> <pre class="example">(setq a 5) +(cond ((eq a 'hack) 'foo) + (t "default")) +⇒ "default" +</pre> +</div> <p>This <code>cond</code> expression returns <code>foo</code> if the value of <code>a</code> is <code>hack</code>, and returns the string <code>"default"</code> otherwise. </p> +</dd> +</dl> <p>Any conditional construct can be expressed with <code>cond</code> or with <code>if</code>. Therefore, the choice between them is a matter of style. For example: </p> <div class="example"> <pre class="example">(if <var>a</var> <var>b</var> <var>c</var>) +≡ +(cond (<var>a</var> <var>b</var>) (t <var>c</var>)) +</pre> +</div><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/Conditionals.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Conditionals.html</a> + </p> +</div> |
