Node: STKLOS Pattern Matching Facilities, Next: , Up: Pattern Matching



STKLOS pattern matching facilities

Only two special forms are provided for this in STKLOS: match-case and match-lambda.

match-case key clause... STKLOS syntax
The argument key may be any expression and each clause has the form
          (pattern s-expression...)
          

Semantics: A match-case expression is evaluated as follows. key is evaluated and the result is compared with each successive pattern. If the pattern in some clause yields a match, then the expressions in that clause are 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 that clause is returned as the result of the match-case expression. If no pattern in any clause matches the datum, then, if there is an else clause, its expressions are evaluated and the result of the last is the result of the whole match-case expression; otherwise the result of the match-case expression 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:

match-lambda clause... STKLOS syntax

It expands into a lambda-expression expecting an argument which, once applied to an expression, behaves exactly like a match-case expression.

          ((match-lambda
             ((?x ?x) 'foo)
             ((?x ?- ?x) 'bar))
           '(a b a))
             => bar