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/pattern_002dmatching-conditional.html | |
new repository
Diffstat (limited to 'devdocs/elisp/pattern_002dmatching-conditional.html')
| -rw-r--r-- | devdocs/elisp/pattern_002dmatching-conditional.html | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/devdocs/elisp/pattern_002dmatching-conditional.html b/devdocs/elisp/pattern_002dmatching-conditional.html new file mode 100644 index 00000000..7221cf8b --- /dev/null +++ b/devdocs/elisp/pattern_002dmatching-conditional.html @@ -0,0 +1,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 © 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> |
