[plt-scheme] match.ss epsilon

From: Bruce Hauman (bhauman at cs.wcu.edu)
Date: Fri Jul 18 12:38:54 EDT 2003

I am not sure that the answer is staring you in the face.

Although it seems that the multiplicity syntax can provide the
functionality that you desire.

One has to verify the upper bound explicitly though.

(define (test x)
  (match x
         (`(,(and 'a size) ... b c)
          (=> fail)
          (or (>= 1 (length size))
         (else #f)))

(test '(a b c)) => #t
(test '(b c))   => #t
(test '(a a b c)) => #f

(define (validate x)
     (match x
               ,(and (or 'blue 'yello 'red) get-size) ...
               car was
               ,(and (or 'not 'very) get-size2) ...
             (=> fail)
             (or (and (>= 1 (length get-size))
                      (>= 1 (length get-size2)))
            (else #f)))

; all eval to #t
(validate  '(the blue car was very cheap))
(validate  '(the car was cheap))
(validate  '(the yello car was cheap))
(validate  '(the red car was not cheap))

; all eval to #f
(validate  '(the blue red car was very cheap))
(validate  '(the car was not very cheap))
(validate  '(the yello yello car was cheap))
(validate  '(the red car not cheap))

This will _not_ work when the set of items matched by the ... pattern
intersects the set of items matched the pattern that follows it.

So, as long as these sets are disjoint this should work fine although it
isn't as attractive as your proposal.

Have a good one,

On Thu, 17 Jul 2003, Neil W. Van Dyke wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> With "match.ss", is there a way to optionally match a subpattern,
> without consuming if there is no match?
> I couldn't find syntax like "optional" or, better yet, "empty":
>     (match '(b c) (((optional 'a) 'b 'c) #t))  =>  #t
>     (match '(b c) (((or 'a empty) 'b 'c) #t))  =>  #t
> Nor, I suppose, an upper-bound extension of the multiplicity syntax:
>     (match '(b c) (('a ..0..1 'b 'c) #t))  =>  #t
> The answer must be staring me in the face...
> (Of course the above example can be done using two match clauses, but I
> want to avoid combinatoric explosion of subpatterns in hairier cases.)
> --
>                                              http://www.neilvandyke.org/

Posted on the users mailing list.