aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2012-12-09 10:36:46 -0500
committerStefan Monnier2012-12-09 10:36:46 -0500
commitf433306af510e86a614e9f9f082b6d2d5f56a968 (patch)
treed30e965b537930a2c7ba999210ae56651562ac26
parent2c066ad3ae3723168aa1f333a98dabcd2ced5024 (diff)
downloademacs-f433306af510e86a614e9f9f082b6d2d5f56a968.tar.gz
emacs-f433306af510e86a614e9f9f082b6d2d5f56a968.zip
* doc/lispref/control.texi (Pattern maching case statement): New node.
-rw-r--r--doc/lispref/ChangeLog4
-rw-r--r--doc/lispref/control.texi104
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 @@
12012-12-09 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * control.texi (Pattern maching case statement): New node.
4
12012-12-06 Stefan Monnier <monnier@iro.umontreal.ca> 52012-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
297To 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
304where each @var{branch} takes the form @code{(@var{upattern}
305@var{body-forms}@dots{})}.
306
307It 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
309corresponding @var{body-forms}. A common use case is to distinguish
310between 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
321In the last clause, @code{code} is a variable that gets bound to the value that
322was returned by @code{(get-return-code x)}.
323
324To give a more complex example, a simple interpreter for a little
325expression 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
339Where @code{`(add ,x ,y)} is a pattern that checks that @code{exp} is a three
340element list starting with the symbol @code{add}, then extracts the second and
341third 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}
343is a number, and @code{_} is the catch-all pattern that matches anything.
344
345There are two kinds of patterns involved in @code{pcase}, called
346@emph{U-patterns} and @emph{Q-patterns}. The @var{upattern} mentioned above
347are U-patterns and can take the following forms:
348
349@table @code
350@item `@var{qpattern}
351This is one of the most common form of patterns. The intention is to mimic the
352backquote macro: this pattern matches those values that could have been built
353by such a backquote expression. Since we're pattern matching rather than
354building a value, the unquote does not indicate where to plug an expression,
355but instead it lets one specify a U-pattern that should match the value at
356that location.
357
358More specifically, a Q-pattern can take the following forms:
359@table @code
360@item (@var{qpattern1} . @var{qpattern2})
361This pattern matches any cons cell whose @code{car} matches @var{QPATTERN1} and
362whose @code{cdr} matches @var{PATTERN2}.
363@item @var{atom}
364This pattern matches any atom @code{equal} to @var{atom}.
365@item ,@var{upattern}
366This pattern matches any object that matches the @var{upattern}.
367@end table
368
369@item @var{symbol}
370A mere symbol in a U-pattern matches anything, and additionally let-binds this
371symbol to the value that it matched, so that you can later refer to it, either
372in the @var{body-forms} or also later in the pattern.
373@item _
374This so-called @emph{don't care} pattern matches anything, like the previous
375one, but unless symbol patterns it does not bind any variable.
376@item (pred @var{pred})
377This pattern matches if the function @var{pred} returns non-@code{nil} when
378called with the object being matched.
379@item (or @var{upattern1} @var{upattern2}@dots{})
380This pattern matches as soon as one of the argument patterns succeeds.
381All argument patterns should let-bind the same variables.
382@item (and @var{upattern1} @var{upattern2}@dots{})
383This pattern matches only if all the argument patterns succeed.
384@item (guard @var{exp})
385This pattern ignores the object being examined and simply succeeds if @var{exp}
386evaluates to non-@code{nil} and fails otherwise. It is typically used inside
387an @code{and} pattern. For example, @code{(and x (guard (< x 10)))}
388is a pattern which matches any number smaller than 10 and let-binds it to
389the 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