next up previous contents index
Next: Cost minimization Up: Algebraic Datatypes and Previous: Instantiating a datatype

Pattern Matching

  The basic pattern matching construct is the match statement. A match statement can be seen as a generalization of C++'s switch statement, in which the expression that follows a case can be a general complex pattern.

Its basic syntax is as follows:

Match ::=Match_Mode ( Exp ) and ¼ and ( Exp )
{ case Match_Rule ¼ case Match_Rule } variant 1
  |  Match_Mode ( Exp ) and ¼ and ( Exp )
{ Match_Rule  - ¼ - Match_Rule } variant 2
  |  Match_Mode ( Exp ) and ¼ and ( Exp ) of variant 3
    Match_Rule  - ¼ - Match_Rule
end match ;
Match_Mode ::=match [ while ]
  |  matchall [ while ]
Match_Rule ::=Pat [ Guard ] [ Cost ] : Match_Action
Guard ::=if Exp
  |  where Exp
  |   - Exp
Cost ::=\ Exp cost expression
Match_Action ::={ Code }
  |  Code for variant 1 only

The 3 syntactic variants of the match statement are equivalent in meaning. A match statement takes a list of match objects and a set of pattern matching rules. The first rule that matches the pattern completely will be executed. Notice that guards can be placed on each rule, and a rule is only applicable if the guards evaluate to true.





Allen Leung
Mon Apr 7 14:33:55 EDT 1997