I've just been playing around a bit with match so please forgive me if I've 
missed some prior discussions regarding issues that are considered settled.

One of my first attempts was to match a vector of two of the same thing using a 
pattern like [a a].  I naively thought that would imply an equality constraint 
between the two items, but in fact each variable matched anything 
independently. I now understand that the pattern variables are considered in 
different scopes, but I think it's confusing.  It's reasonable that you want 
something faster than unification for simple literal patterns, but this to my 
mind is a special case that's likely to trip people up.

My work-around is to use something like this:

(match [x y]
        [a (b :when #(= % x))] :match
        :else :no-match)

But that doesn't look very nice, and it gets worse with multiple pattern 
variables.  Still, it doesn't look too hard to make that sort of conversion 
automatically so maybe I'll try to write a macro.  (Famous last words. :-)

In any case, if using multiple pattern variables of the same name does not 
imply an equality constraint, I suggest that it be considered an error to reuse 
a pattern variable.  It's better to throw than to yield an unexpected match.

Regarding OR patterns, I didn't really like the infix notation, (1 | 2).  As a 
lisper, I'd prefer to use something like (or 1 2), or maybe even a Clojure set 
notation: #{1 2}. I'm guessing you already thought about this and made your 
decision on syntax, but I thought I'd throw it out there.

For guards, I wonder if the extra parens are really necessary.  Couldn't the 
:when bind tightly to the previous pattern variable?  Like Clojure FOR 
comprehensions.  I think it would be easier to read that way.  Same comment 
applies to :as.  To cover the rare case of matching a literal :when or :as, you 
could quote it or use it as the first item in an OR pattern.

As others have suggested, I agree that returning nil when nothing matches makes 
sense.  That was my original expectation.

It was common when testing to wrap a let around the match so I made a little 
macro to save a few characters.  Free for anyone who wants it.  :-)

(defmacro match-let [bindings & body]
  (let [bindvars# (take-nth 2 bindings)]
    `(let ~bindings
       (match [~@bindvars#]
                ~@body))))


Steve Miner
stevemi...@gmail.com

-- 
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