I've been working with core.match some this week and finding it pretty
nice.  However, I have a common case (for me) that is not well handled
right now via core.match: matching symbols. Say that I wrote a match
like this:

;; translate (+ x (+ y z)) to (+ x y z)
(let [e '(+ 1 (+ 2 3))]
  (match [e]
       [([+ x ([+ y z] :seq)] :seq)] (+ x y z)))

You will see this error:
Pattern row 1: Pattern row reuses wildcards in [([+ x ([+ y
z] :seq)] :seq)].  The following wildcards are ambiguous: +.  There's
no guarantee that the matched values will be same.  Rename the
occurrences uniquely.

Any symbol inside a pattern row is treated as a bind variable.  + is a
symbol.  You can achieve this with guards:

(defn +? [s] (= '+ s))

(let [e '(+ 1 (+ 2 3))]
  (match ['(+ 1 (+ 2 3))]
       [([(_o1 :when +?) x ([(_o2 :when +?) y z] :seq)] :seq)] (list
'+ x y z)))

but, yuck.  I can imagine using the reserved ()'s with additional keys
(:symbol or :sym) to do symbol matching like (:symbol +) but also,
yuck.  The simplest idea I came up with was:

(let [e '(+ 1 (+ 2 3))]
  (match [e]
       [(['+ x (['+ y z] :seq)] :seq)] ('+ x y z)))

These come through as (quote x) although the error reporting goes a
little off the rails:
Pattern row 1: Pattern row reuses wildcards in [([(quote +) x ([(quote
+) y z] :seq)] :seq)].  The following wildcards are ambiguous: quote.
There's no guarantee that the matched values will be same.  Rename the
occurrences uniquely.

However, that seems fixable and you could then use (quote x) as a
signal to do symbol matching.  If I can figure out what the hell I'm
doing in core.match then I'd be happy to work on a patch.

Thoughts?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to