On Thu, Jan 29, 2015 at 10:46:45AM +0000, László Török wrote:
> One thing that isn't obvious to me, how it should work when matching more
> than one pattern.
I can't seem to find it anywhere in the core.match wiki, but I'm fairly
sure it tries them in order, returning the first one that matches.
> In the following example, the expression returns 3 or 4 depending on which
> pattern comes first:
>
> (require '[clojure.core.match :as cm])
>
> (cm/match [:a true false]
> [_ _ true] 1
> [:a _ true] 2
> [:a true false] 4
> [:a true _] 3)
This would return 4 because the 4 matches before the 3. If you were to
swap their orders then it would return 3.
(cm/match [:a true false]
[_ _ true] 1
[:a _ true] 2
[:a true _] 3
[:a true false] 4)
;; => 3
> Is it correct to assume, that the algorithm constructs an decision tree
> that will hit a minimum amount of patterns, however, if there are more than
> one matching clauses, its result is an arbitrary choice of the possible
> decisions?
It will try to create a decision tree to efficiently check the patterns,
but they will be checked (at least conceptually) in order.
So this:
(cm/match [:a true false]
[_ _ _] 1
[:a _ true] 2
[:a true _] 3
[:a true false] 4)
;; => 1
doesn't get past the first pattern, despite the fact that the later ones
are more specific.
I hope that helps!
Carlo
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.