I'm trying to define a pattern matching function:
test[x__?MatchQ[Equal[_, _]]] := 1;
Now I give it a series of inputs, and expect each to output 1
:
test[1 == a, 1 == b]
test[1 == a, 1 == a]
test[1 == a, a == 1]
test[1 == a]
...none of which evaluate to 1
. I can't see what I've done wrong here. So I try the following:
MatchQ[Equal[_, _]][a == b]
Which gives False
. I don't understand why. I look at the FullForm
of a==b
and it gives back Equal[a,b]
which should match with the pattern Equal[_,_]
.
However, for reasons beyond me, this expression...
MatchQ[Equal[_, __]][a == b]
...evaluates to True
.
I have no idea what's going on here or why my test
pattern match doesn't work. Can someone please explain what is going on? i.e.
- Why doesn't my
test
pattern match work as intended? - How can I pattern match a sequence of equalities
a==b, c==d ...
? - Why does
MatchQ[Equal[_,__]][a==b]
yieldTrue
while replacing__
with_
yields false?
Equal[_, _]
. It'sTrue
. So you'll need aHoldPattern
. b)?
is a high-precedence operator so you need?(MatchQ[HoldPattern[Equal[_, _]]])
. Even better though would just bex__Equal
. It's most concise and the fastest. $\endgroup$_==__
so that explains the behaviour of that pattern match. You can write up a brief answer if you want. $\endgroup$HoldPattern[_ == _] ..
$\endgroup$