core.match gotcha

119 views
Skip to first unread message

Chas Emerick

unread,
Jan 6, 2013, 9:18:05 AM1/6/13
to Clojure
I've recently started using core.match, which has been quite pleasant and successful; thank you to David Nolen, and all others that have contributed.

The only hiccup I've had has been around how core.match incorporates bindings from local scope into pattern rows. A stupid example demonstrating the (potential) confusion:

=> (match [[:k]]
[[x]] x)
:k

vs.

=> (let [x 12]
(match [[:k]]
[[x]] x))
nil

I was assuming that bindings established by the wildcards in pattern rows shadowed any other local bindings. It took me a while to realize that, e.g. in the example above, `x` was being interpolated into `[[x]]` to yield a pattern row of `[[12]]`. Now that I know that, all's well; but, it took me perhaps longer than it should have to figure it out.

All this is to say, perhaps a relevant note in the (really excellent) overview wiki page[1] might be helpful.

Thanks,

- Chas

[1] https://github.com/clojure/core.match/wiki/Overview

Ambrose Bonnaire-Sergeant

unread,
Jan 6, 2013, 9:52:37 AM1/6/13
to clo...@googlegroups.com
I'll add a note to the wiki.

Thanks,
Ambrose


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Ambrose Bonnaire-Sergeant

unread,
Jan 6, 2013, 10:23:45 AM1/6/13
to clo...@googlegroups.com
I included a section on how symbols work as patterns.

Interestingly, the old design page still has some good stuff, including an explanation of this particular issue.
I'm not sure how much is still relevant.


Thanks,
Ambrose

Ambrose Bonnaire-Sergeant

unread,
Jan 6, 2013, 10:48:51 AM1/6/13
to clo...@googlegroups.com
On a related note, combining a quoted symbol and a named wildcard pattern seems to be buggy.

clojure.core.match=> (match 'my-sym a a)
#<CompilerException java.lang.RuntimeException: Unable to resolve symbol: ocr-3612 in this context, compiling:(REPL:79)>
clojure.core.match=> (macroexpand-1 '(match 'my-sym a a))
(clojure.core/let [a ocr-3620] a)

Thanks,
Ambrose

Chas Emerick

unread,
Jan 6, 2013, 11:16:07 AM1/6/13
to clo...@googlegroups.com
On Jan 6, 2013, at 10:48 AM, Ambrose Bonnaire-Sergeant wrote:

On a related note, combining a quoted symbol and a named wildcard pattern seems to be buggy.

clojure.core.match=> (match 'my-sym a a)
#<CompilerException java.lang.RuntimeException: Unable to resolve symbol: ocr-3612 in this context, compiling:(REPL:79)>
clojure.core.match=> (macroexpand-1 '(match 'my-sym a a))
(clojure.core/let [a ocr-3620] a)

Yeah, I've hit that, but presumed I was just doing something wrong. :-)

I included a section on how symbols work as patterns.

Perfect, reads very well.  I'm glad I didn't just add something, it would not have been as clear or succinct.

Interestingly, the old design page still has some good stuff, including an explanation of this particular issue.
I'm not sure how much is still relevant.


Ach, I hadn't thought to look at what else might be lurking in the wiki!  The 'Local Bindings' section on the design page would have made it all obvious.

Thanks!

- Chas

Chas Emerick

unread,
Jan 6, 2013, 3:43:13 PM1/6/13
to Clojure
On Jan 6, 10:48 am, Ambrose Bonnaire-Sergeant
<abonnaireserge...@gmail.com> wrote:
> On a related note, combining a quoted symbol and a named wildcard pattern
> seems to be buggy.
>
> clojure.core.match=> (match 'my-sym a a)
> #<CompilerException java.lang.RuntimeException: Unable to resolve symbol:
> ocr-3612 in this context, compiling:(REPL:79)>
> clojure.core.match=> (macroexpand-1 '(match 'my-sym a a))
> (clojure.core/let [a ocr-3620] a)

Ticket opened here FWIW:

http://dev.clojure.org/jira/browse/MATCH-66

- Chas
Reply all
Reply to author
Forward
0 new messages