summaryrefslogtreecommitdiff
path: root/devdocs/elisp/pattern_002dmatching-conditional.html
blob: 7221cf8bb140e7b9318781748fcf8a35bd97d8fa (plain)
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
 <h3 class="section">Pattern-Matching Conditional</h3>   <p>Aside from the four basic conditional forms, Emacs Lisp also has a pattern-matching conditional form, the <code>pcase</code> macro, a hybrid of <code>cond</code> and <code>cl-case</code> (see <a href="https://www.gnu.org/software/emacs/manual/html_node/cl/Conditionals.html#Conditionals">Conditionals</a> in <cite>Common Lisp Extensions</cite>) that overcomes their limitations and introduces the <em>pattern matching programming style</em>. The limitations that <code>pcase</code> overcomes are: </p> <ul> <li> The <code>cond</code> form chooses among alternatives by evaluating the predicate <var>condition</var> of each of its clauses (see <a href="conditionals">Conditionals</a>). The primary limitation is that variables let-bound in <var>condition</var> are not available to the clause’s <var>body-forms</var>. <p>Another annoyance (more an inconvenience than a limitation) is that when a series of <var>condition</var> predicates implement equality tests, there is a lot of repeated code. (<code>cl-case</code> solves this inconvenience.) </p> </li>
<li> The <code>cl-case</code> macro chooses among alternatives by evaluating the equality of its first argument against a set of specific values. <p>Its limitations are two-fold: </p> <ol> <li> The equality tests use <code>eql</code>. </li>
<li> The values must be known and written in advance. </li>
</ol> <p>These render <code>cl-case</code> unsuitable for strings or compound data structures (e.g., lists or vectors). (<code>cond</code> doesn’t have these limitations, but it has others, see above.) </p>
</li>
</ul> <p>Conceptually, the <code>pcase</code> macro borrows the first-arg focus of <code>cl-case</code> and the clause-processing flow of <code>cond</code>, replacing <var>condition</var> with a generalization of the equality test which is a variant of <em>pattern matching</em>, and adding facilities so that you can concisely express a clause’s predicate, and arrange to share let-bindings between a clause’s predicate and <var>body-forms</var>. </p> <p>The concise expression of a predicate is known as a <em>pattern</em>. When the predicate, called on the value of the first arg, returns non-<code>nil</code>, we say that “the pattern matches the value” (or sometimes “the value matches the pattern”). </p> <table class="menu" border="0" cellspacing="0"> <tr>
<td align="left" valign="top">• <a href="pcase-macro" accesskey="1">The <code>pcase</code> macro</a>
</td>
<td> </td>
<td align="left" valign="top">Includes examples and caveats. </td>
</tr> <tr>
<td align="left" valign="top">• <a href="extending-pcase" accesskey="2">Extending <code>pcase</code></a>
</td>
<td> </td>
<td align="left" valign="top">Define new kinds of patterns. </td>
</tr> <tr>
<td align="left" valign="top">• <a href="backquote-patterns" accesskey="3">Backquote-Style Patterns</a>
</td>
<td> </td>
<td align="left" valign="top">Structural patterns matching. </td>
</tr> <tr>
<td align="left" valign="top">• <a href="destructuring-with-pcase-patterns" accesskey="4">Destructuring with pcase Patterns</a>
</td>
<td> </td>
<td align="left" valign="top">Using pcase patterns to extract subfields. </td>
</tr> </table><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 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/Pattern_002dMatching-Conditional.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Pattern_002dMatching-Conditional.html</a>
  </p>
</div>