match.texi 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2010, 2011 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}.
  40. The same object can be matched against a simpler pattern:
  41. @example
  42. (let ((l '(hello (world))))
  43. (match l
  44. ((x y)
  45. (values x y))))
  46. @result{} hello
  47. @result{} (world)
  48. @end example
  49. Here pattern @code{(x y)} matches any two-element list, regardless of
  50. the types of these elements. Pattern variables @var{x} and @var{y} are
  51. bound to, respectively, the first and second element of @var{l}.
  52. The pattern matcher is defined as follows:
  53. @deffn {Scheme Syntax} match exp clause1 clause2 @dots{}
  54. Match object @var{exp} against the patterns in @var{clause1}
  55. @var{clause2} @dots{} in the order in which they appear. Return the
  56. value produced by the first matching clause. If no clause matches,
  57. throw an exception with key @code{match-error}.
  58. Each clause has the form @code{(pattern body1 body2 @dots{})}. Each
  59. @var{pattern} must follow the syntax described below. Each body is an
  60. arbitrary Scheme expression, possibly referring to pattern variables of
  61. @var{pattern}.
  62. @end deffn
  63. @c FIXME: Document other forms:
  64. @c
  65. @c exp ::= ...
  66. @c | (match exp clause ...)
  67. @c | (match-lambda clause ...)
  68. @c | (match-lambda* clause ...)
  69. @c | (match-let ((pat exp) ...) body)
  70. @c | (match-let* ((pat exp) ...) body)
  71. @c | (match-letrec ((pat exp) ...) body)
  72. @c | (match-define pat exp)
  73. @c
  74. @c clause ::= (pat body) | (pat => exp)
  75. The syntax and interpretation of patterns is as follows:
  76. @verbatim
  77. patterns: matches:
  78. pat ::= identifier anything, and binds identifier
  79. | _ anything
  80. | () the empty list
  81. | #t #t
  82. | #f #f
  83. | string a string
  84. | number a number
  85. | character a character
  86. | 'sexp an s-expression
  87. | 'symbol a symbol (special case of s-expr)
  88. | (pat_1 ... pat_n) list of n elements
  89. | (pat_1 ... pat_n . pat_{n+1}) list of n or more
  90. | (pat_1 ... pat_n pat_n+1 ooo) list of n or more, each element
  91. of remainder must match pat_n+1
  92. | #(pat_1 ... pat_n) vector of n elements
  93. | #(pat_1 ... pat_n pat_n+1 ooo) vector of n or more, each element
  94. of remainder must match pat_n+1
  95. | #&pat box
  96. | ($ record-name pat_1 ... pat_n) a record
  97. | (= field pat) a ``field'' of an object
  98. | (and pat_1 ... pat_n) if all of pat_1 thru pat_n match
  99. | (or pat_1 ... pat_n) if any of pat_1 thru pat_n match
  100. | (not pat_1 ... pat_n) if all pat_1 thru pat_n don't match
  101. | (? predicate pat_1 ... pat_n) if predicate true and all of
  102. pat_1 thru pat_n match
  103. | (set! identifier) anything, and binds setter
  104. | (get! identifier) anything, and binds getter
  105. | `qp a quasi-pattern
  106. | (identifier *** pat) matches pat in a tree and binds
  107. identifier to the path leading
  108. to the object that matches pat
  109. ooo ::= ... zero or more
  110. | ___ zero or more
  111. | ..1 1 or more
  112. quasi-patterns: matches:
  113. qp ::= () the empty list
  114. | #t #t
  115. | #f #f
  116. | string a string
  117. | number a number
  118. | character a character
  119. | identifier a symbol
  120. | (qp_1 ... qp_n) list of n elements
  121. | (qp_1 ... qp_n . qp_{n+1}) list of n or more
  122. | (qp_1 ... qp_n qp_n+1 ooo) list of n or more, each element
  123. of remainder must match qp_n+1
  124. | #(qp_1 ... qp_n) vector of n elements
  125. | #(qp_1 ... qp_n qp_n+1 ooo) vector of n or more, each element
  126. of remainder must match qp_n+1
  127. | #&qp box
  128. | ,pat a pattern
  129. | ,@pat a pattern
  130. @end verbatim
  131. The names @code{quote}, @code{quasiquote}, @code{unquote},
  132. @code{unquote-splicing}, @code{?}, @code{_}, @code{$}, @code{and},
  133. @code{or}, @code{not}, @code{set!}, @code{get!}, @code{...}, and
  134. @code{___} cannot be used as pattern variables.
  135. Here is a more complex example:
  136. @example
  137. (use-modules (srfi srfi-9))
  138. (let ()
  139. (define-record-type person
  140. (make-person name friends)
  141. person?
  142. (name person-name)
  143. (friends person-friends))
  144. (letrec ((alice (make-person "Alice" (delay (list bob))))
  145. (bob (make-person "Bob" (delay (list alice)))))
  146. (match alice
  147. (($ person name (= force (($ person "Bob"))))
  148. (list 'friend-of-bob name))
  149. (_ #f))))
  150. @result{} (friend-of-bob "Alice")
  151. @end example
  152. @noindent
  153. Here the @code{$} pattern is used to match a SRFI-9 record of type
  154. @var{person} containing two or more slots. The value of the first slot
  155. is bound to @var{name}. The @code{=} pattern is used to apply
  156. @code{force} on the second slot, and then checking that the result
  157. matches the given pattern. In other words, the complete pattern matches
  158. any @var{person} whose second slot is a promise that evaluates to a
  159. one-element list containing a @var{person} whose first slot is
  160. @code{"Bob"}.
  161. Please refer to the @code{ice-9/match.upstream.scm} file in your Guile
  162. installation for more details.
  163. Guile also comes with a pattern matcher specifically tailored to SXML
  164. trees, @xref{sxml-match}.