match.texi 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
  4. @c See the file guile.texi for copying conditions.
  5. @c
  6. @c The pattern syntax is taken from the documentation available in
  7. @c Andrew K. Wright's implementation of `match.scm', which is in the
  8. @c public domain. See Guile before commit
  9. @c d967913f05301a35573c5d3f7217d0994bbb1016 (Thu Jun 17 2010) or
  10. @c <http://www.cs.indiana.edu/scheme-repository/code.match.html>.
  11. @c FIXME: This section is a bit rough on the edges. The introduction
  12. @c could be improved, e.g., by adding examples.
  13. @node Pattern Matching
  14. @section Pattern Matching
  15. @cindex pattern matching
  16. @cindex (ice-9 match)
  17. The @code{(ice-9 match)} module provides a @dfn{pattern matcher},
  18. written by Alex Shinn, and compatible with Andrew K. Wright's pattern
  19. matcher found in many Scheme implementations.
  20. @cindex pattern variable
  21. A pattern matcher can match an object against several patterns and
  22. extract the elements that make it up. Patterns can represent any Scheme
  23. object: lists, strings, symbols, records, etc. They can optionally contain
  24. @dfn{pattern variables}. When a matching pattern is found, an
  25. expression associated with the pattern is evaluated, optionally with all
  26. pattern variables bound to the corresponding elements of the object:
  27. @example
  28. (let ((l '(hello (world))))
  29. (match l ;; <- the input object
  30. (('hello (who)) ;; <- the pattern
  31. who))) ;; <- the expression evaluated upon matching
  32. @result{} world
  33. @end example
  34. In this example, list @var{l} matches the pattern @code{('hello (who))},
  35. because it is a two-element list whose first element is the symbol
  36. @code{hello} and whose second element is a one-element list. Here
  37. @var{who} is a pattern variable. @code{match}, the pattern matcher,
  38. locally binds @var{who} to the value contained in this one-element
  39. list---i.e., the symbol @code{world}. An error would be raised if
  40. @var{l} did not match the pattern.
  41. The same object can be matched against a simpler pattern:
  42. @example
  43. (let ((l '(hello (world))))
  44. (match l
  45. ((x y)
  46. (values x y))))
  47. @result{} hello
  48. @result{} (world)
  49. @end example
  50. Here pattern @code{(x y)} matches any two-element list, regardless of
  51. the types of these elements. Pattern variables @var{x} and @var{y} are
  52. bound to, respectively, the first and second element of @var{l}.
  53. Patterns can be composed, and nested. For instance, @code{...}
  54. (ellipsis) means that the previous pattern may be matched zero or more
  55. times in a list:
  56. @example
  57. (match lst
  58. (((heads tails ...) ...)
  59. heads))
  60. @end example
  61. @noindent
  62. This expression returns the first element of each list within @var{lst}.
  63. For proper lists of proper lists, it is equivalent to @code{(map car
  64. lst)}. However, it performs additional checks to make sure that
  65. @var{lst} and the lists therein are proper lists, as prescribed by the
  66. pattern, raising an error if they are not.
  67. Compared to hand-written code, pattern matching noticeably improves
  68. clarity and conciseness---no need to resort to series of @code{car} and
  69. @code{cdr} calls when matching lists, for instance. It also improves
  70. robustness, by making sure the input @emph{completely} matches the
  71. pattern---conversely, hand-written code often trades robustness for
  72. conciseness. And of course, @code{match} is a macro, and the code it
  73. expands to is just as efficient as equivalent hand-written code.
  74. The pattern matcher is defined as follows:
  75. @deffn {Scheme Syntax} match exp clause1 clause2 @dots{}
  76. Match object @var{exp} against the patterns in @var{clause1}
  77. @var{clause2} @dots{} in the order in which they appear. Return the
  78. value produced by the first matching clause. If no clause matches,
  79. throw an exception with key @code{match-error}.
  80. Each clause has the form @code{(pattern body1 body2 @dots{})}. Each
  81. @var{pattern} must follow the syntax described below. Each body is an
  82. arbitrary Scheme expression, possibly referring to pattern variables of
  83. @var{pattern}.
  84. @end deffn
  85. @c FIXME: Document other forms:
  86. @c
  87. @c exp ::= ...
  88. @c | (match exp clause ...)
  89. @c | (match-lambda clause ...)
  90. @c | (match-lambda* clause ...)
  91. @c | (match-let ((pat exp) ...) body)
  92. @c | (match-let* ((pat exp) ...) body)
  93. @c | (match-letrec ((pat exp) ...) body)
  94. @c | (match-define pat exp)
  95. @c
  96. @c clause ::= (pat body) | (pat => exp)
  97. The syntax and interpretation of patterns is as follows:
  98. @verbatim
  99. patterns: matches:
  100. pat ::= identifier anything, and binds identifier
  101. | _ anything
  102. | () the empty list
  103. | #t #t
  104. | #f #f
  105. | string a string
  106. | number a number
  107. | character a character
  108. | 'sexp an s-expression
  109. | 'symbol a symbol (special case of s-expr)
  110. | (pat_1 ... pat_n) list of n elements
  111. | (pat_1 ... pat_n . pat_{n+1}) list of n or more
  112. | (pat_1 ... pat_n pat_n+1 ooo) list of n or more, each element
  113. of remainder must match pat_n+1
  114. | #(pat_1 ... pat_n) vector of n elements
  115. | #(pat_1 ... pat_n pat_n+1 ooo) vector of n or more, each element
  116. of remainder must match pat_n+1
  117. | #&pat box
  118. | ($ record-name pat_1 ... pat_n) a record
  119. | (= field pat) a ``field'' of an object
  120. | (and pat_1 ... pat_n) if all of pat_1 thru pat_n match
  121. | (or pat_1 ... pat_n) if any of pat_1 thru pat_n match
  122. | (not pat_1 ... pat_n) if all pat_1 thru pat_n don't match
  123. | (? predicate pat_1 ... pat_n) if predicate true and all of
  124. pat_1 thru pat_n match
  125. | (set! identifier) anything, and binds setter
  126. | (get! identifier) anything, and binds getter
  127. | `qp a quasi-pattern
  128. | (identifier *** pat) matches pat in a tree and binds
  129. identifier to the path leading
  130. to the object that matches pat
  131. ooo ::= ... zero or more
  132. | ___ zero or more
  133. | ..1 1 or more
  134. quasi-patterns: matches:
  135. qp ::= () the empty list
  136. | #t #t
  137. | #f #f
  138. | string a string
  139. | number a number
  140. | character a character
  141. | identifier a symbol
  142. | (qp_1 ... qp_n) list of n elements
  143. | (qp_1 ... qp_n . qp_{n+1}) list of n or more
  144. | (qp_1 ... qp_n qp_n+1 ooo) list of n or more, each element
  145. of remainder must match qp_n+1
  146. | #(qp_1 ... qp_n) vector of n elements
  147. | #(qp_1 ... qp_n qp_n+1 ooo) vector of n or more, each element
  148. of remainder must match qp_n+1
  149. | #&qp box
  150. | ,pat a pattern
  151. | ,@pat a pattern
  152. @end verbatim
  153. The names @code{quote}, @code{quasiquote}, @code{unquote},
  154. @code{unquote-splicing}, @code{?}, @code{_}, @code{$}, @code{and},
  155. @code{or}, @code{not}, @code{set!}, @code{get!}, @code{...}, and
  156. @code{___} cannot be used as pattern variables.
  157. Here is a more complex example:
  158. @example
  159. (use-modules (srfi srfi-9))
  160. (let ()
  161. (define-record-type person
  162. (make-person name friends)
  163. person?
  164. (name person-name)
  165. (friends person-friends))
  166. (letrec ((alice (make-person "Alice" (delay (list bob))))
  167. (bob (make-person "Bob" (delay (list alice)))))
  168. (match alice
  169. (($ person name (= force (($ person "Bob"))))
  170. (list 'friend-of-bob name))
  171. (_ #f))))
  172. @result{} (friend-of-bob "Alice")
  173. @end example
  174. @noindent
  175. Here the @code{$} pattern is used to match a SRFI-9 record of type
  176. @var{person} containing two or more slots. The value of the first slot
  177. is bound to @var{name}. The @code{=} pattern is used to apply
  178. @code{force} on the second slot, and then checking that the result
  179. matches the given pattern. In other words, the complete pattern matches
  180. any @var{person} whose second slot is a promise that evaluates to a
  181. one-element list containing a @var{person} whose first slot is
  182. @code{"Bob"}.
  183. Please refer to the @code{ice-9/match.upstream.scm} file in your Guile
  184. installation for more details.
  185. Guile also comes with a pattern matcher specifically tailored to SXML
  186. trees, @xref{sxml-match}.