[racket] matching optional keyword + value

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Fri Apr 27 08:18:34 EDT 2012

On Fri, Apr 27, 2012 at 8:03 AM, Laurent <laurent.orseau at gmail.com> wrote:
> Hi,
>
> I'm trying to match form declarations that can contain optional
> keyword-followed-by-something (more precisely matching "defforms"
> declarations).
> It looks like (id [#:kw1 val1] x [#:kw2 val2] ....) where the [] means
> optional.
> For example the list to match may be '(a b) or '(a #:kw1 1 b) or  '(a b
> #:kw2 2) or '(a #:kw1 3 b #:kw2 4), etc.
>
> But I couldn't figure out how to say "match 2 things one after the other, 0
> or 1 time".
> Something like that could do it:
> (match L
>   [`(,id ,@(list '#:kw1 v1) ... ,x ,@(list '#:kw2 v2) ...  ....)
>   ....])
>
> but nothing I tried got close to it (the ... are quoted and don't like being
> unquoted).
> Google gave me that result:
> http://stackoverflow.com/questions/7061533/match-form-in-racket-scheme-question-about-matching-sequences
> but that's not really what I'm after.
>
> Is there any way to do that with match patterns without doing some
> recurrence over `match' myself (and not writing all the possible composition
> of with/without each keyword)?

`match' doesn't support the repetition constraints and other
extensions to ... patterns that `syntax-parse' does.  `list-no-order'
patterns might be helpful here, though.

If you're processing the contents of a `defform' call, probably the
right thing to do is just just use `syntax-parse' -- it's great for
this sort of thing.
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.