Pattern matching is a key feature of most modern functional programming
languages since it allows clean and secure code to be
written. Internally, ``pattern-matching forms'' should be translated
(compiled) into cascades of ``elementary tests'' where code is made as
efficient as possible, avoiding redundant tests; Bigloo's ``pattern
matching compiler'' provides this. The technique used is described in
details in [QueinnecGeffroy92], and the code generated can be considered
optimal 
1 due to the way this ``pattern
compiler'' was obtained.
The ``pattern language'' allows the expression of a wide variety of patterns,
including:
- Non-linear patterns: pattern variables can appear more than
once, allowing comparison of subparts of the datum (through eq?)
 
 
- Recursive patterns on lists: for example, checking that the
datum is a list of zero or more as followed by zero or morebs.
 
 
- Pattern matching on lists as well as on vectors and structures, 
and record types.
 
 
| 7.1 Bigloo pattern matching facilities
 | 
Only two special forms are provided for this in Bigloo: 
match-case 
and 
match-lambda.
| 
The argument| match-case key clause... | bigloo syntax |  keymay be any expression and eachclausehas the form
 
 
Semantics:  A| (patterns-expression...) |  match-caseexpression is evaluated as
follows.keyis evaluated and the result is compared with each
successive pattern. If the pattern in someclauseyields a match, then
the expressions in thatclauseare evaluated from left to right in an
environment where the pattern variables are bound to the corresponding
subparts of the datum, and the result of the last expression in thatclauseis returned as the result of thematch-caseexpression.
If nopatternin anyclausematches the datum, then, if there is anelseclause, its expressions are evaluated and the result of the last
is the result of the wholematch-caseexpression; otherwise the result
of thematch-caseexpression is unspecified.
 The equality predicate used is
 eq?.
 
 
| (match-case '(a b a)
   ((?x ?x) 'foo)
   ((?x ?- ?x) 'bar))
   => bar
 |  | 
The following syntax is also available:
| 
It expands into a lambda-expression expecting an argument which, once
applied to an expression, behaves exactly like a| match-lambda clause... | bigloo syntax |  match-caseexpression.
 
 
| ((match-lambda
   ((?x ?x) 'foo)
   ((?x ?- ?x) 'bar))
 '(a b a))
   => bar
 |  | 
 
The syntax for <pattern> is:
| <pattern> ==>                Matches:
 <atom>                    the <atom>.
| (
 kwote<atom>)            any expressioneq?to<atom>.
| (and<pat1> ... <patn>)   if all of<patmatch.
| (i>or<pat1> ... ...<patn>) if any of<pat1>through<patn>matches.
| (not<pat>)               if<pat>doesn't match.
| (?<predicate>)           if<predicate>is true.
| (<pat1> ... <patn>)       a list of n elements. Here,...is a
                            meta-character denoting a finite repetition
                            of patterns.
| <pat> ...                 a (possibly empty) repetition
                            of<pat>in a list.
| #(<pat> ... <patn>)       a vector ofnelements.
| #{<struct> <pat> ... }    a structure.
|?<id>                     anything, and bindsidas a variable.
|?-anything.
|??-any (possibly empty) repetition of anything
                            in a list.
|???-any end of list. | 
Remark:  and, or, not, check and 
kwote must be
quoted in order to be treated as literals. This is the only justification
for having the 
kwote pattern since, by convention, any atom which is
not a keyword is quoted.
- ?-matches any s-expr
- amatches the atom- 'a.
- ?amatches any expression, and binds the variable- ato
this expression.
 
 
- (? integer?)matches any integer
- (a (a b))matches the only list- '(a (a b)).
- ???-can only appear at the end of a list, and always succeeds.
For instance,- (a ???-)is equivalent to- (a . ?-).
 
 
- when occurring in a list, ??-matches any sequence of anything:(a ??- b)matches any list whosecarisaand lastcarisb.
 
 
- (a ...)matches any list of- a's, possibly empty.
- (?x ?x)matches any list of length 2 whose- caris 
 eq to its- cadr
 
 
- ((and (not a) ?x) ?x)matches any list of length 2 whose- caris not eq to- 'abut is eq to its- cadr
 
 
- #(?- ?- ???-)matches any vector whose length is at least 2.
- #{foo (?- . ?-) (? integer?)}matches any structure or
record- foowhose first and second fields are respectively a pair and an
integer. You can provide only the fields you want to test. The order is not
relevant.
Remark:??- and 
... patterns can not appear
inside a vector, where you should use 
???-: For example, 
#(a ??- b) or 
#(a...) are invalid patterns, whereas 
#(a ???-) is valid and matches any vector whose first element 
is the atom 
a.