I also answered on SO, but you just want to do more in the `app` pattern:<br><br><div><div>#lang racket</div><div><br></div><div>(struct foo (a b c) #:transparent)</div><div><br></div><div>(define-match-expander foo-string</div><div> (λ (stx)</div><div> (syntax-case stx ()</div><div> [(_ result)</div><div> #'(app (λ (v) (apply (λ (_ a b c) (foo a b c))</div><div> (regexp-match #px"^(.+)\\s(.+)\\s(.+)$" v)))</div><div> result)])))</div><div><br></div><div>(define (foo-from-str str)</div><div> (match str</div><div> [(foo-string value) value]</div><div> [_ #f]))</div><div><br></div><div>(foo-from-str "1 2 3")</div></div><div><br></div><div>Sam</div><br><div class="gmail_quote">On Fri Jan 16 2015 at 4:42:56 PM Alexis King <<a href="mailto:lexi.lambda@gmail.com">lexi.lambda@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Yes, chandler already suggested something like that on #racket. I didn’t think of that, but it should work. It’d be nice if there were a cleaner way to do it, but if you want to post that as an answer on SO for the imaginary internet points, I’d accept it.</div></div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Jan 16, 2015, at 13:38, Alexander D. Knauth <<a href="mailto:alexander@knauth.org" target="_blank">alexander@knauth.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>Would this work for what you want?</div><div><div><font face="Courier New">#lang racket</font></div><div><font face="Courier New">(require rackunit)</font></div><div><font face="Courier New">(struct foo (a b c))</font></div><div><font face="Courier New">(define-match-expander foo-string</font></div><div><font face="Courier New"> (λ (stx)</font></div><div><font face="Courier New"> (syntax-case stx ()</font></div><div><font face="Courier New"> [(_ result)</font></div><div><font face="Courier New"> #'(app (λ (v)</font></div><div><font face="Courier New"> (match v</font></div><div><font face="Courier New"> [(pregexp #px"^(.+)<a>\\s(.+)\\s(.+)$</a>" (list _ a b c))</font></div><div><font face="Courier New"> (foo a b c)]</font></div><div><font face="Courier New"> [_ #f]))</font></div><div><font face="Courier New"> (? foo? result))])))</font></div><div><font face="Courier New">(define (foo-from-str str)</font></div><div><font face="Courier New"> (match str</font></div><div><font face="Courier New"> [(foo-string value) value]</font></div><div><font face="Courier New"> [_ #f]))</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(check-match (foo-from-str "1 2 3") (foo "1" "2" "3"))</font></div></div><div><br></div><br><div><div>On Jan 16, 2015, at 4:24 PM, Alexis King <<a href="mailto:lexi.lambda@gmail.com" target="_blank">lexi.lambda@gmail.com</a>> wrote:</div><br><blockquote type="cite"><div style="word-wrap:break-word">I just posted <a href="http://stackoverflow.com/q/27992566/465378" target="_blank">a question on Stack Overflow asking about using racket/match</a>. 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.<div><div><br></div><div><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(34,34,34);font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:19.5px;background-color:rgb(255,255,255)">In a <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">racket/match</code> pattern, I want to match some values, then create a struct that wraps them. For example, take the following (contrived) code:</p><pre style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(238,238,238)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">struct foo </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(138,74,11)">define</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">foo-from-string str</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">match str
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">[</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">pregexp </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">#</span><span style="margin:0px;padding:0px;border:0px">px</span><span style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"^(.+)<a>\\s(.+)\\s(.+)$</a>"</span><span style="margin:0px;padding:0px;border:0px"> a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">foo a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">]</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">[</span><span style="margin:0px;padding:0px;border:0px">_ </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">#</span><span style="margin:0px;padding:0px;border:0px">f</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">]</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span></code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(34,34,34);font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:19.5px;background-color:rgb(255,255,255)">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:</p><pre style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(238,238,238)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">struct foo </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(138,74,11)">define</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">foo-from-str str</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">match str
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">[</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">foo-string value</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px"> value</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">]</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">[</span><span style="margin:0px;padding:0px;border:0px">_ </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">#</span><span style="margin:0px;padding:0px;border:0px">f</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">]</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span></code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(34,34,34);font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:19.5px;background-color:rgb(255,255,255)">That is, it should automatically match a string that satisfies the regex, then store the values into a <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">foo</code> struct on success and bind it to <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">value</code>. I tried writing something like the following to implement this:</p><pre style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;color:rgb(57,51,24);word-wrap:normal;background-color:rgb(238,238,238)"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(138,74,11)">define</span><span style="margin:0px;padding:0px;border:0px">-match-expander foo-string
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">λ</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">stx</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">syntax-case stx </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">[</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">_ result</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">#</span><span style="margin:0px;padding:0px;border:0px;color:rgb(149,56,56)">'</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">and </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">pregexp </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">#</span><span style="margin:0px;padding:0px;border:0px">px</span><span style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"^(.+)<a>\\s(.+)\\s(.+)$</a>"</span><span style="margin:0px;padding:0px;border:0px"> a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">app </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">λ</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">v</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">(</span><span style="margin:0px;padding:0px;border:0px">foo a b c</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span><span style="margin:0px;padding:0px;border:0px"> result</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">))</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">]</span><span style="margin:0px;padding:0px;border:0px;color:rgb(20,86,128)">)))</span></code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(34,34,34);font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:19.5px;background-color:rgb(255,255,255)">Unfortunately, this fails because <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">a</code>, <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">b</code>, and <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">c</code>, are unbound when the function passed to the <code style="margin:0px;padding:1px 5px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif;white-space:pre-wrap;background-color:rgb(238,238,238)">app</code> 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?</p></div></div></div>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br></blockquote></div><br></div></div></blockquote></div><br></div>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a><br>
</blockquote></div>