[racket] How can I use the results of a pattern match to produce a value in a match-expander?

From: Alexis King (lexi.lambda at gmail.com)
Date: Fri Jan 16 16:24:42 EST 2015

I just posted a question on Stack Overflow asking about using racket/match <http://stackoverflow.com/q/27992566/465378>. Since I’m not sure how active the racket tag is on SO, I figured I’d also post it here. For those who don’t want to look at the SO question, here’s the question’s contents.

In a racket/match pattern, I want to match some values, then create a struct that wraps them. For example, take the following (contrived) code:

(struct foo (a b c))

(define (foo-from-string str)
  (match str
    [(pregexp #px"^(.+)\\s(.+)\\s(.+)$" a b c)
     (foo a b c)]
    [_ #f]))
I frequently find myself matching this list of three elements in other patterns, then creating a struct from it. Therefore, I wanted to simplify this by writing a custom match expander. Ideally, it would work like this:

(struct foo (a b c))

(define (foo-from-str str)
  (match str
    [(foo-string value) value]
    [_ #f]))
That is, it should automatically match a string that satisfies the regex, then store the values into a foo struct on success and bind it to value. I tried writing something like the following to implement this:

(define-match-expander foo-string
  (λ (stx)
    (syntax-case stx ()
      [(_ result)
       #'(and (pregexp #px"^(.+)\\s(.+)\\s(.+)$" a b c)
              (app (λ (v) (foo a b c)) result))])))
Unfortunately, this fails because a, b, and c, are unbound when the function passed to the app pattern gets called. Is there any way to implement such a match expander so that it can perform some arbitrary procedure on the matched values?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150116/e940bdfb/attachment.html>

Posted on the users mailing list.