[racket-dev] for/match construct?

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Tue Oct 25 09:42:10 EDT 2011

I'm considering putting in some effort to generalize the binding construct in for-clauses so that we can have for[*]/match/X macros. This will require modifying and exposing define-for-variants (due to circularity in requiring match in for). Does anyone object? I'll of course need the code reviewed by those more familiar with for, but I'm hoping I don't need to mess with complicated implementation details such as taints.

Example:
Here the significantly new behavior is that name should match in the for/match clauses in order to run the body.

(for* ([mch (redex-match L some-pat some-term)]
       [bnds (match-bindings mch)]
       [use-names (in-value use-names)])
  (for/match ([(bind name exp) bnds]
              [name use-names])
    ...code...))

What this looks like without for/match is

(for* ([mch (redex-match L some-pat some-term)]
       [bnds (match-bindings mch)]
       [use-names (in-value use-names)])
  (for ([bnd bnds]
        [name use-names]
        #:when (match* (bnd name) [((bind nm _) nm) #t] [(_ _) #f]))
    (match-define-values ((name exp) name) (values bnd name))
    ...code...))

Notice the unnecessary second match.

-Ian


Posted on the dev mailing list.