diff options
| author | Stefan Monnier | 2012-12-09 10:36:46 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2012-12-09 10:36:46 -0500 |
| commit | f433306af510e86a614e9f9f082b6d2d5f56a968 (patch) | |
| tree | d30e965b537930a2c7ba999210ae56651562ac26 | |
| parent | 2c066ad3ae3723168aa1f333a98dabcd2ced5024 (diff) | |
| download | emacs-f433306af510e86a614e9f9f082b6d2d5f56a968.tar.gz emacs-f433306af510e86a614e9f9f082b6d2d5f56a968.zip | |
* doc/lispref/control.texi (Pattern maching case statement): New node.
| -rw-r--r-- | doc/lispref/ChangeLog | 4 | ||||
| -rw-r--r-- | doc/lispref/control.texi | 104 |
2 files changed, 108 insertions, 0 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 210897f6de3..b0f5e9bced0 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2012-12-09 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * control.texi (Pattern maching case statement): New node. | ||
| 4 | |||
| 1 | 2012-12-06 Stefan Monnier <monnier@iro.umontreal.ca> | 5 | 2012-12-06 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 6 | ||
| 3 | * customize.texi (Variable Definitions): Mention the default :group | 7 | * customize.texi (Variable Definitions): Mention the default :group |
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 489e5cc5b22..00b0a75a3e2 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi | |||
| @@ -285,6 +285,110 @@ For example: | |||
| 285 | @end group | 285 | @end group |
| 286 | @end example | 286 | @end example |
| 287 | 287 | ||
| 288 | @menu | ||
| 289 | * Pattern maching case statement:: | ||
| 290 | @end menu | ||
| 291 | |||
| 292 | @node Pattern maching case statement | ||
| 293 | @subsection Pattern maching case statement | ||
| 294 | @cindex pcase | ||
| 295 | @cindex pattern matching | ||
| 296 | |||
| 297 | To compare a particular value against various possible cases, the macro | ||
| 298 | @code{pcase} can come handy. It takes the following form: | ||
| 299 | |||
| 300 | @example | ||
| 301 | (pcase @var{exp} @var{branch}1 @var{branch}2 @var{branch}3 @dots{}) | ||
| 302 | @end example | ||
| 303 | |||
| 304 | where each @var{branch} takes the form @code{(@var{upattern} | ||
| 305 | @var{body-forms}@dots{})}. | ||
| 306 | |||
| 307 | It will first evaluate @var{exp} and then compare the value against each | ||
| 308 | @var{upattern} to see which @var{branch} to use, after which it will run the | ||
| 309 | corresponding @var{body-forms}. A common use case is to distinguish | ||
| 310 | between a few different constant values: | ||
| 311 | |||
| 312 | @example | ||
| 313 | (pcase (get-return-code x) | ||
| 314 | (`success (message "Done!")) | ||
| 315 | (`would-block (message "Sorry, can't do it now")) | ||
| 316 | (`read-only (message "The shmliblick is read-only")) | ||
| 317 | (`access-denied (message "You do not have the needed rights")) | ||
| 318 | (code (message "Unknown return code %S" code))) | ||
| 319 | @end example | ||
| 320 | |||
| 321 | In the last clause, @code{code} is a variable that gets bound to the value that | ||
| 322 | was returned by @code{(get-return-code x)}. | ||
| 323 | |||
| 324 | To give a more complex example, a simple interpreter for a little | ||
| 325 | expression language could look like: | ||
| 326 | |||
| 327 | @example | ||
| 328 | (defun evaluate (exp env) | ||
| 329 | (pcase exp | ||
| 330 | (`(add ,x ,y) (+ (evaluate x env) (evaluate y env))) | ||
| 331 | (`(call ,fun ,arg) (funcall (evaluate fun) (evaluate arg env))) | ||
| 332 | (`(fn ,arg ,body) (lambda (val) | ||
| 333 | (evaluate body (cons (cons arg val) env)))) | ||
| 334 | ((pred numberp) exp) | ||
| 335 | ((pred symbolp) (cdr (assq exp env))) | ||
| 336 | (_ (error "Unknown expression %S" exp)))) | ||
| 337 | @end example | ||
| 338 | |||
| 339 | Where @code{`(add ,x ,y)} is a pattern that checks that @code{exp} is a three | ||
| 340 | element list starting with the symbol @code{add}, then extracts the second and | ||
| 341 | third elements and binds them to the variables @code{x} and @code{y}. | ||
| 342 | @code{(pred numberp)} is a pattern that simply checks that @code{exp} | ||
| 343 | is a number, and @code{_} is the catch-all pattern that matches anything. | ||
| 344 | |||
| 345 | There are two kinds of patterns involved in @code{pcase}, called | ||
| 346 | @emph{U-patterns} and @emph{Q-patterns}. The @var{upattern} mentioned above | ||
| 347 | are U-patterns and can take the following forms: | ||
| 348 | |||
| 349 | @table @code | ||
| 350 | @item `@var{qpattern} | ||
| 351 | This is one of the most common form of patterns. The intention is to mimic the | ||
| 352 | backquote macro: this pattern matches those values that could have been built | ||
| 353 | by such a backquote expression. Since we're pattern matching rather than | ||
| 354 | building a value, the unquote does not indicate where to plug an expression, | ||
| 355 | but instead it lets one specify a U-pattern that should match the value at | ||
| 356 | that location. | ||
| 357 | |||
| 358 | More specifically, a Q-pattern can take the following forms: | ||
| 359 | @table @code | ||
| 360 | @item (@var{qpattern1} . @var{qpattern2}) | ||
| 361 | This pattern matches any cons cell whose @code{car} matches @var{QPATTERN1} and | ||
| 362 | whose @code{cdr} matches @var{PATTERN2}. | ||
| 363 | @item @var{atom} | ||
| 364 | This pattern matches any atom @code{equal} to @var{atom}. | ||
| 365 | @item ,@var{upattern} | ||
| 366 | This pattern matches any object that matches the @var{upattern}. | ||
| 367 | @end table | ||
| 368 | |||
| 369 | @item @var{symbol} | ||
| 370 | A mere symbol in a U-pattern matches anything, and additionally let-binds this | ||
| 371 | symbol to the value that it matched, so that you can later refer to it, either | ||
| 372 | in the @var{body-forms} or also later in the pattern. | ||
| 373 | @item _ | ||
| 374 | This so-called @emph{don't care} pattern matches anything, like the previous | ||
| 375 | one, but unless symbol patterns it does not bind any variable. | ||
| 376 | @item (pred @var{pred}) | ||
| 377 | This pattern matches if the function @var{pred} returns non-@code{nil} when | ||
| 378 | called with the object being matched. | ||
| 379 | @item (or @var{upattern1} @var{upattern2}@dots{}) | ||
| 380 | This pattern matches as soon as one of the argument patterns succeeds. | ||
| 381 | All argument patterns should let-bind the same variables. | ||
| 382 | @item (and @var{upattern1} @var{upattern2}@dots{}) | ||
| 383 | This pattern matches only if all the argument patterns succeed. | ||
| 384 | @item (guard @var{exp}) | ||
| 385 | This pattern ignores the object being examined and simply succeeds if @var{exp} | ||
| 386 | evaluates to non-@code{nil} and fails otherwise. It is typically used inside | ||
| 387 | an @code{and} pattern. For example, @code{(and x (guard (< x 10)))} | ||
| 388 | is a pattern which matches any number smaller than 10 and let-binds it to | ||
| 389 | the variable @code{x}. | ||
| 390 | @end table | ||
| 391 | |||
| 288 | @node Combining Conditions | 392 | @node Combining Conditions |
| 289 | @section Constructs for Combining Conditions | 393 | @section Constructs for Combining Conditions |
| 290 | 394 | ||