<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">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><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 16, 2015, at 13:38, Alexander D. Knauth <<a href="mailto:alexander@knauth.org" class="">alexander@knauth.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Would this work for what you want?</div><div class=""><div class=""><font face="Courier New" class="">#lang racket</font></div><div class=""><font face="Courier New" class="">(require rackunit)</font></div><div class=""><font face="Courier New" class="">(struct foo (a b c))</font></div><div class=""><font face="Courier New" class="">(define-match-expander foo-string</font></div><div class=""><font face="Courier New" class=""> (λ (stx)</font></div><div class=""><font face="Courier New" class=""> (syntax-case stx ()</font></div><div class=""><font face="Courier New" class=""> [(_ result)</font></div><div class=""><font face="Courier New" class=""> #'(app (λ (v)</font></div><div class=""><font face="Courier New" class=""> (match v</font></div><div class=""><font face="Courier New" class=""> [(pregexp #px"^(.+)<a href="smb://s(.+)//s(.+)$" class="">\\s(.+)\\s(.+)$</a>" (list _ a b c))</font></div><div class=""><font face="Courier New" class=""> (foo a b c)]</font></div><div class=""><font face="Courier New" class=""> [_ #f]))</font></div><div class=""><font face="Courier New" class=""> (? foo? result))])))</font></div><div class=""><font face="Courier New" class="">(define (foo-from-str str)</font></div><div class=""><font face="Courier New" class=""> (match str</font></div><div class=""><font face="Courier New" class=""> [(foo-string value) value]</font></div><div class=""><font face="Courier New" class=""> [_ #f]))</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">(check-match (foo-from-str "1 2 3") (foo "1" "2" "3"))</font></div></div><div class=""><br class=""></div><br class=""><div class=""><div class="">On Jan 16, 2015, at 4:24 PM, Alexis King <<a href="mailto:lexi.lambda@gmail.com" class="">lexi.lambda@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I just posted <a href="http://stackoverflow.com/q/27992566/465378" class="">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 class=""><div class=""><br class=""></div><div class=""><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);" class="">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);" class="">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 class="lang-lisp prettyprint prettyprinted" 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;" class=""><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">struct foo </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">))</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(138, 74, 11);">define</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">foo-from-string str</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">match str
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">[</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">pregexp </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">#</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">px</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: rgb(128, 0, 0);">"^(.+)<a href="smb://s(.+)//s(.+)$" class="">\\s(.+)\\s(.+)$</a>"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">foo a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">]</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">_ </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">#</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">f</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">]</span><span class="clo" 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);" class="">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 class="lang-lisp prettyprint prettyprinted" 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;" class=""><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">struct foo </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">))</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(138, 74, 11);">define</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">foo-from-str str</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">match str
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">[</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">foo-string value</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> value</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">]</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">[</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">_ </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">#</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">f</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">]</span><span class="clo" 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);" class="">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);" class="">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);" class="">value</code>. I tried writing something like the following to implement this:</p><pre class="lang-lisp prettyprint prettyprinted" 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;" class=""><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(138, 74, 11);">define</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">-match-expander foo-string
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">λ</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">stx</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">syntax-case stx </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">[</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">_ result</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">#</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: rgb(149, 56, 56);">'</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">and </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">pregexp </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">#</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">px</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: rgb(128, 0, 0);">"^(.+)<a href="smb://s(.+)//s(.+)$" class="">\\s(.+)\\s(.+)$</a>"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">app </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">λ</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">v</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="opn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">foo a b c</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">))</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> result</span><span class="clo" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">))</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(20, 86, 128);">]</span><span class="clo" 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);" class="">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);" class="">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);" class="">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);" class="">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);" class="">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 class=""> Racket Users list:<br class=""> <a href="http://lists.racket-lang.org/users" class="">http://lists.racket-lang.org/users</a><br class=""></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>