<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body><div>I think I've buried the problem inside an inaccurate analogy.
</div><div><br></div><div>I'm wondering if there's a generalized idiom for matching an item in a dispatch pattern WITHOUT passing it through as one of the arguments.
</div><div><br></div><div>For instance. Suppose I have a magic.html page that I generate with a route, so I set up a dispatch pattern like this:
</div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;
</div><div><div>#lang racket
</div><div>(require web-server/dispatch
</div><div>web-server/http/request-structs
</div><div>net/url)
</div><div><br></div><div>(define (url->request u)
</div><div>(make-request #"GET" (string->url u) empty
</div><div>(delay empty) #f "1.2.3.4" 80 "4.3.2.1"))
</div><div><br></div><div>(define-values (test-dispatch _)
</div><div>(dispatch-rules
</div><div>[((string-arg) ... "magic.html") (λ(req . args) (print args))]))
</div><div><br></div><div>(test-dispatch (url->request "http://host.com/some/path/to/magic.html"))
</div></div><div>;;;;;;;;;;;;;;;;;;;;;
</div><div><br></div><div>This dispatch pattern will match the given URL and return as the arguments:
</div><div><br></div><div>'(("some" "path" "to"))
</div><div><br></div><div>So far, so good. But suppose I want to put the name of the magic page in a variable, magic-page-name, and match on that instead:
</div><div><br></div><div>(define magic-page-name "magic.html")
</div><div><br></div><div>So how do I adjust the dispatch pattern? If I do this:
</div><div><br></div><div>((string-arg) ... magic-page-name)<br>
</div><div><br></div><div>It's a syntax error, of course. So instead I change the pattern to:
</div><div><br></div><div>((string-arg) ... (? (λ(x)(equal? x magic-page-name))))<br>
</div><div><br></div><div>This seems to work, except that for arguments, I now get:
</div><div><br></div><div>'(("some" "path" "to") "magic.html")<br>
</div><div><br></div><div>What I would prefer is to match on magic-page-name without passing through the result as an argument (i.e., get the same behavior as when I was using the string literal)
</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div class="unibox-signature"></div><div><br></div><blockquote type="cite"><div>On Jan 21, 2014, at 9:10 AM, Jay McCarthy <jay.mccarthy@gmail.com> wrote:
</div><div><br></div><div style="orphans: auto; text-align: start; widows: auto;">Hi Matthew,
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">In your 'match' examples, you are printing out the result of "matched"<br>which is bound by (and matched ...) on the outside of the match<br>pattern, so it is equivalent to
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">(define matched (list ....))<br>(match matched<br>[...inside... ....])
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">This idiom doesn't really have anything to do with what dispatch-rules<br>does, because dispatch-rules doesn't have "and". Instead,<br>dispatch-rules behaves more like a normal "match" where only the names<br>are bound/passed-to-the-function.
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">Jay
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;"></div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">On Sat, Jan 18, 2014 at 9:59 PM, Matthew Butterick <mb@mbtype.com> wrote:
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><blockquote style="orphans: auto; text-align: start; widows: auto;"><div>If you run this code:
</div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>#lang racket<br>(require web-server/dispatch<br>web-server/http/request-structs<br>net/url)
</div><div><br></div><div>(define (url->request u)<br>(make-request #"GET" (string->url u) empty<br>(delay empty) #f "1.2.3.4" 80 "4.3.2.1"))
</div><div><br></div><div>(define-values (test-dispatch sum-url)<br>(dispatch-rules<br>[((? string?) "foo" (? string?)) test-route]<br>[((? string?) (? (λ(x) (equal? x "bar"))) (? string?)) test-route]))
</div><div><br></div><div>(define (test-route req . args)<br>(displayln (format "args = ~v" args)))
</div><div><br></div><div>(test-dispatch (url->request "http://url.com/first-string/foo/second-string.html"))<br>(test-dispatch (url->request "http://url.com/first-string/bar/second-string.html"))<br>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
</div><div><br></div><div>You'll get this:
</div><div><br></div><div>args = '("first-string" "second-string.html")<br>args = '("first-string" "bar" "second-string.html")
</div><div><br></div><div><br></div><div>Whereas if you use these patterns with regular match, like so:
</div><div><br></div><div>(match (list "first-string" "foo" "second-string.html")<br>[(and matched (list (? string?) "foo" (? string?))) matched])
</div><div><br></div><div>(match (list "first-string" "bar" "second-string.html")<br>[(and matched (list (? string?) (? (λ(x) (equal? x "bar"))) (? string?))) matched])
</div><div><br></div><div>You'll get:
</div><div><br></div><div>'("first-string" "foo" "second-string.html")<br>'("first-string" "bar" "second-string.html")
</div><div><br></div><div><br></div><div>Thus my question: why doesn't the literal "foo" in the first pattern under dispatch-rules become one of the matched items, as it does with regular match?
</div><div><br></div><div><br></div><div>____________________<br>Racket Users list:<br>http://lists.racket-lang.org/users
</div><div><br></div></blockquote><div style="orphans: auto; text-align: start; widows: auto;"></div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;"></div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;"></div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">--<br>Jay McCarthy <jay@cs.byu.edu><br>Assistant Professor / Brigham Young University<br>http://faculty.cs.byu.edu/~jay
</div><div style="orphans: auto; text-align: start; widows: auto;"><br></div><div style="orphans: auto; text-align: start; widows: auto;">"The glory of God is Intelligence" - D&C 93
</div><br class="Apple-interchange-newline"></blockquote></body></html>