previous up next     contents index
Next: Patterns Up: Pattern Matching for Scheme Previous: Contents

Definition

The complete syntax of the pattern matching expressions follows:

 
exp ::= (match exp clause ...) 
             (match-lambda clause ...) 
             (match-lambda* clause ...) 
             (match-let ([pat exp] ...) body) 
             (match-let* ([pat exp] ...) body) 
             (match-letrec ([pat exp] ...) body) 
             (match-let var ([pat exp] ...) body) 
             (match-define pat exp) 
 [2ex] clause  [pat body]  tex2html_wrap_inline542  [pat (=> identifier) body] 

Figure 0.1 gives the full syntax for patterns.

 

  figure34


Figure 0.1: Pattern Syntax

The next subsection describes the various patterns.

The match-lambda and match-lambda* forms are convenient combinations of match and lambda, and can be explained as follows:

tabular180

where x is a unique variable. The match-lambda form is convenient when defining a single argument function that immediately destructures its argument. The match-lambda* form constructs a function that accepts any number of arguments; the patterns of match-lambda* should be lists.

The match-let, match-let*, match-letrec, and match-define forms generalize Scheme's let, let*, letrec, and define expressions to allow patterns in the binding position rather than just variables. For example, the following expression:

(match-let ([(x y z) (list 1 2 3)])  $body$)
binds x to 1, y to 2, and z to 3 in body. These forms are convenient for destructuring the result of a function that returns multiple values. As usual for letrec and define, pattern variables bound by match-letrec and match-define should not be used in computing the bound value.

The match, match-lambda, and match-lambda* forms allow the optional syntax (=> identifier) between the pattern and the body of a clause. When the pattern match for such a clause succeeds, the identifier is bound to a failure procedure of zero arguments within the body. If this procedure is invoked, it jumps back to the pattern matching expression, and resumes the matching process as if the pattern had failed to match. The body must not mutate the object being matched, otherwise unpredictable behavior may result.




previous up next     contents index
Next: Patterns Up: Pattern Matching for Scheme Previous: Contents

PLT