1
$\begingroup$

Why did MatchQ[a + b + c, Plus[x_, y_] is considered true?

In my opinion, it should only consider 2 elements within Plus, such as MatchQ[a + b, Plus[_, _]] true.

Why is this MatchQ[a + b, Plus[_, _]] false?

Where did I get wrong? Please point out.

$\endgroup$
5
  • $\begingroup$ @LeonidShifrin Adding HoldPattern is simply not working. And How can I make MatchQ[a + b, Plus[_, _]] true? $\endgroup$
    – kile
    Feb 29, 2020 at 13:02
  • 2
    $\begingroup$ MatchQ[a + b, HoldPattern[Plus[_, _]]] works for me. $\endgroup$ Feb 29, 2020 at 13:06
  • $\begingroup$ @LeonidShifrin I thought it was like this. MatchQ[(a + b + c), Plus[_, _, _]] // HoldPattern I am new to HoldPattern. Sorry for this. $\endgroup$
    – kile
    Feb 29, 2020 at 13:08
  • $\begingroup$ HoldPattern is wrapped around the pattern which you don't want to evaluate. It may be a good idea for you to read more on the language core / basics. You can search this site for resources, there are plenty of recommendations. $\endgroup$ Feb 29, 2020 at 13:11
  • $\begingroup$ See the examples in the docs for HoldPattern $\endgroup$
    – Michael E2
    Feb 29, 2020 at 15:22

0

Browse other questions tagged or ask your own question.