plt-match.ss: Pattern Matching

To load: (require (lib "plt-match.ss"))

This library provide a pattern matcher just like Chapter 27, but with an improved syntax for patterns. This pattern syntax uses keywords for each of the different pattern matches, making the syntax both extensible and more clear. It also provides extensions that are unavailable in match.ss.

The only difference between plt-match.ss and match.ss is the syntax of the patterns and the set of available patterns. The forms where the patterns may appear are identical.

Figure 2 gives the full syntax for patterns.

pat     ::= identifier [not ooo]              Match anything, bind identifier as a variable              
         |  _                                 Match anything                                             
         |  literal                           Match literal                                              
         |  'datum                            Match equal? datum                                         
         |  'symbol                           Match equal? symbol (special case of datum)                
         |  (list lvp ...)                    Match sequence of lvps                                     
         |  (list-rest lvp ... pat)           Match sequence of lvps consed onto a pat                   
         |  (list-no-order pat ... lvp)       Match arguments in a list in any order                     
         |  (vector lvp ... lvp)              Match vector of pats                                       
         |  (hash-table (pat pat) ...)        Match hash table mapping pats to pats                      
         |  (hash-table (pat pat) ... ooo)    Match hash table mapping pats to pats                      
         |  (box pat)                         Match boxed pat                                            
         |  (struct struct-name (pat ...))    Match struct-name instance with matching fields            
         |  (regexp rx-expr)                  Match string using (regexp-match rx-expr ...)                     
         |  (regexp rx-expr pat)              Match string to rx-expr, pat matches regexp result           
         |  (pregexp prx-expr)                Match string using (pregexp-match prx-expr ...)                   
         |  (pregexp prx-expr pat)            Match string to prx-expr, pat matches pregexp result         
         |  (and pat ...)                     Match when all pats match                                  
         |  (or pat ...)                      Match when any pat match                                   
         |  (not pat ...)                     Match when no pat match                                    
         |  (app expr pat)                    Match when result of applying expr to the value matches pat
         |  (? pred-expr pat ...)             Match if pred-expr is true on the value, and all pats match
         |  (set! identifier)                 Match anything, bind identifier as a setter                
         |  (get! identifier)                 Match anything, bind identifier as a getter                
         |  `qp                               Match a quasipattern                                       
literal ::= ()                                Match the empty list                                       
         |  #t                                Match true                                                 
         |  #f                                Match false                                                
         |  string                            Match equal? string                                        
         |  number                            Match equal? number                                        
         |  character                         Match equal? character                                     
lvp     ::= pat ooo                           Greedily match pat instances                               
         |  pat                               Match pat                                                  
ooo     ::= ...                               Zero or more (where ... is a keyword)                      
         |  ___                               Zero or more                                               
         |  ..k                               k or more, where k is a non-negative integer               
         |  __k                               k or more, where k is a non-negative integer               
qp      ::= literal                           Match literal                                              
         |  identifier                        Match equal? symbol                                        
         |  (qp ...)                          Match sequences of qps                                     
         |  (qp ... . qp)                     Match sequence of qps consed onto a qp                     
         |  (qp ... ooo)                      Match qps consed onto a repeated qp                        
         |  #(qp ...)                         Match vector of qps                                        
         |  #&qp                              Match boxed qp                                             
         |  ,pat                              Match pat                                                  
         |  ,@(list lvp ...)                  Match lvp sequence, spliced                                
         |  ,@(list-rest lvp ... pat)         Match lvp sequence plus pat, spliced                       
         |  ,@'qp                             Match list-matching qp, spliced                            

Figure 2:  Pattern Syntax