diff options
| author | Eli Zaretskii | 2016-01-29 11:40:31 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2016-01-29 11:40:31 +0200 |
| commit | d7a93efd0e512ce145ee696561054f8065fe03ab (patch) | |
| tree | d8e4bf461da23df400dbe3fe5c2e9c2872294641 | |
| parent | 7257815df133593e754f6396972f46fd2083374e (diff) | |
| download | emacs-d7a93efd0e512ce145ee696561054f8065fe03ab.tar.gz emacs-d7a93efd0e512ce145ee696561054f8065fe03ab.zip | |
Minor improvements to 'pcase' documentation
* doc/lispref/control.texi (Pattern matching case statement):
Improve the documentation of 'pcase' per comments. See two
discussion threads on emacs-devel@gnu.org for the details:
http://lists.gnu.org/archive/html/emacs-devel/2016-01/msg01335.html
http://lists.gnu.org/archive/html/emacs-devel/2016-01/msg01336.html.
| -rw-r--r-- | doc/lispref/control.texi | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 6fa802d9fdd..3f48c458c02 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi | |||
| @@ -304,15 +304,15 @@ is useful to select alternatives based on more general conditions that | |||
| 304 | distinguish between broad classes of values. The @code{pcase} macro | 304 | distinguish between broad classes of values. The @code{pcase} macro |
| 305 | allows you to choose between alternatives based on matching the value | 305 | allows you to choose between alternatives based on matching the value |
| 306 | of an expression against a series of patterns. A pattern can be a | 306 | of an expression against a series of patterns. A pattern can be a |
| 307 | literal value (comparison to literal values is what @code{cond} does), | 307 | literal value (for comparisons to literal values you'd use |
| 308 | or it can be a more general description of the expected structure of | 308 | @code{cond}), or it can be a more general description of the expected |
| 309 | the expression's value. | 309 | structure of the expression's value. |
| 310 | 310 | ||
| 311 | @defmac pcase expression &rest clauses | 311 | @defmac pcase expression &rest clauses |
| 312 | Evaluate @var{expression} and choose among an arbitrary number of | 312 | Evaluate @var{expression} and choose among an arbitrary number of |
| 313 | alternatives based on the value of @var{expression}. The possible | 313 | alternatives based on the value of @var{expression}. The possible |
| 314 | alternatives are specified by @var{clauses}, each of which must be a | 314 | alternatives are specified by @var{clauses}, each of which must be a |
| 315 | list of the form @code{(@var{pattern} @var{body-forms})}. | 315 | list of the form @code{(@var{pattern} @var{body-forms}@dots{})}. |
| 316 | @code{pcase} tries to match the value of @var{expression} to the | 316 | @code{pcase} tries to match the value of @var{expression} to the |
| 317 | @var{pattern} of each clause, in textual order. If the value matches, | 317 | @var{pattern} of each clause, in textual order. If the value matches, |
| 318 | the clause succeeds; @code{pcase} then evaluates its @var{body-forms}, | 318 | the clause succeeds; @code{pcase} then evaluates its @var{body-forms}, |
| @@ -328,7 +328,7 @@ Note: In the description of the patterns below, we use ``the value | |||
| 328 | being matched'' to refer to the value of the @var{expression} that is | 328 | being matched'' to refer to the value of the @var{expression} that is |
| 329 | the first argument of @code{pcase}. | 329 | the first argument of @code{pcase}. |
| 330 | 330 | ||
| 331 | A UPattern can have one of the following forms: | 331 | A UPattern can have the following forms: |
| 332 | 332 | ||
| 333 | @table @code | 333 | @table @code |
| 334 | 334 | ||
| @@ -337,7 +337,8 @@ Matches if the value being matched is @code{equal} to @var{val}. | |||
| 337 | @item @var{atom} | 337 | @item @var{atom} |
| 338 | Matches any @var{atom}, which can be a keyword, a number, or a string. | 338 | Matches any @var{atom}, which can be a keyword, a number, or a string. |
| 339 | (These are self-quoting, so this kind of UPattern is actually a | 339 | (These are self-quoting, so this kind of UPattern is actually a |
| 340 | shorthand for @code{'@var{atom}}.) | 340 | shorthand for @code{'@var{atom}}.) Note that a string or a float |
| 341 | matches any string or float with the same contents/value. | ||
| 341 | @item _ | 342 | @item _ |
| 342 | Matches any value. This is known as @dfn{don't care} or @dfn{wildcard}. | 343 | Matches any value. This is known as @dfn{don't care} or @dfn{wildcard}. |
| 343 | @item @var{symbol} | 344 | @item @var{symbol} |
| @@ -362,7 +363,8 @@ Matches if the specified @var{expression} matches the specified | |||
| 362 | an @emph{arbitrary} expression, not just the expression that is the | 363 | an @emph{arbitrary} expression, not just the expression that is the |
| 363 | first argument to @code{pcase}. (It is called @code{let} because | 364 | first argument to @code{pcase}. (It is called @code{let} because |
| 364 | @var{upattern} can bind symbols to values using the @var{symbol} | 365 | @var{upattern} can bind symbols to values using the @var{symbol} |
| 365 | UPattern.) | 366 | UPattern. For example: |
| 367 | @w{@code{((or `(key . ,val) (let val 5)) val)}}.) | ||
| 366 | @item (app @var{function} @var{upattern}) | 368 | @item (app @var{function} @var{upattern}) |
| 367 | Matches if @var{function} applied to the value being matched returns a | 369 | Matches if @var{function} applied to the value being matched returns a |
| 368 | value that matches @var{upattern}. This is like the @code{pred} | 370 | value that matches @var{upattern}. This is like the @code{pred} |
| @@ -407,24 +409,27 @@ Here's an illustrative example of using UPatterns: | |||
| 407 | (code (message "Unknown return code %S" code))) | 409 | (code (message "Unknown return code %S" code))) |
| 408 | @end example | 410 | @end example |
| 409 | 411 | ||
| 410 | The QPatterns are more powerful. They allow matching the value of the | 412 | In addition, you can use backquoted patterns that are more powerful. |
| 411 | @var{expression} that is the first argument of @code{pcase} against | 413 | They allow matching the value of the @var{expression} that is the |
| 412 | specifications of its @emph{structure}. For example, you can specify | 414 | first argument of @code{pcase} against specifications of its |
| 413 | that the value must be a list of 2 elements whose first element is a | 415 | @emph{structure}. For example, you can specify that the value must be |
| 414 | string and the second element is a number. QPatterns can have one of | 416 | a list of 2 elements whose first element is a specific string and the |
| 415 | the following forms: | 417 | second element is any value with a backquoted pattern like |
| 418 | @code{`("first" ,second-elem)}. | ||
| 419 | |||
| 420 | Backquoted patterns have the form @code{`@var{qpattern}} where | ||
| 421 | @var{qpattern} can have the following forms: | ||
| 416 | 422 | ||
| 417 | @table @code | 423 | @table @code |
| 418 | @item `(@var{qpattern1} . @var{qpattern2}) | 424 | @item (@var{qpattern1} . @var{qpattern2}) |
| 419 | Matches if the value being matched is a cons cell whose @code{car} | 425 | Matches if the value being matched is a cons cell whose @code{car} |
| 420 | matches @var{qpattern1} and whose @code{cdr} matches @var{qpattern2}. | 426 | matches @var{qpattern1} and whose @code{cdr} matches @var{qpattern2}. |
| 421 | @item `[@var{qpattern1} @var{qpattern2} @dots{} @var{qpatternm}] | 427 | This readily generalizes to backquoted lists as in |
| 428 | @w{@code{(@var{qpattern1} @var{qpattern2} @dots{})}}. | ||
| 429 | @item [@var{qpattern1} @var{qpattern2} @dots{} @var{qpatternm}] | ||
| 422 | Matches if the value being matched is a vector of length @var{m} whose | 430 | Matches if the value being matched is a vector of length @var{m} whose |
| 423 | @code{0}..@code{(@var{m}-1)}th elements match @var{qpattern1}, | 431 | @code{0}..@code{(@var{m}-1)}th elements match @var{qpattern1}, |
| 424 | @var{qpattern2} @dots{} @var{qpatternm}, respectively. | 432 | @var{qpattern2} @dots{} @var{qpatternm}, respectively. |
| 425 | @item `(,@var{upattern1} ,@var{upattern2} @dots{}) | ||
| 426 | Matches if the value being matched is a list whose elements match the | ||
| 427 | corresponding @var{upattern1}, @var{upattern2}, etc. | ||
| 428 | @item @var{atom} | 433 | @item @var{atom} |
| 429 | Matches if corresponding element of the value being matched is | 434 | Matches if corresponding element of the value being matched is |
| 430 | @code{equal} to the specified @var{atom}. | 435 | @code{equal} to the specified @var{atom}. |
| @@ -433,6 +438,13 @@ Matches if the corresponding element of the value being matched | |||
| 433 | matches the specified @var{upattern}. | 438 | matches the specified @var{upattern}. |
| 434 | @end table | 439 | @end table |
| 435 | 440 | ||
| 441 | Note that uses of QPatterns can be expressed using only UPatterns, as | ||
| 442 | QPatterns are implemented on top of UPatterns using | ||
| 443 | @code{pcase-defmacro}, described below. However, using QPatterns will | ||
| 444 | in many cases lead to a more readable code. | ||
| 445 | @c FIXME: There should be an example here showing how a 'pcase' that | ||
| 446 | @c uses QPatterns can be rewritten using UPatterns. | ||
| 447 | |||
| 436 | @end defmac | 448 | @end defmac |
| 437 | 449 | ||
| 438 | Here is an example of using @code{pcase} to implement a simple | 450 | Here is an example of using @code{pcase} to implement a simple |
| @@ -476,8 +488,11 @@ Additional UPatterns can be defined using the @code{pcase-defmacro} | |||
| 476 | macro. | 488 | macro. |
| 477 | 489 | ||
| 478 | @defmac pcase-defmacro name args &rest body | 490 | @defmac pcase-defmacro name args &rest body |
| 479 | Define a new UPattern for @code{pcase}. The UPattern will have the | 491 | Define a new kind of UPattern for @code{pcase}. The new UPattern will |
| 480 | form @code{(@var{name} @var{args})}. | 492 | be invoked as @code{(@var{name} @var{actual-args})}. The @var{body} |
| 493 | should describe how to rewrite the UPattern @var{name} into some other | ||
| 494 | UPattern. The rewriting will be the result of evaluating @var{body} | ||
| 495 | in an environment where @var{args} are bound to @var{actual-args}. | ||
| 481 | @end defmac | 496 | @end defmac |
| 482 | 497 | ||
| 483 | @node Combining Conditions | 498 | @node Combining Conditions |