1
$\begingroup$

I am learning to work with MatchQ for pattern matching in expressions. For instance:

MatchQ[x + y, Plus[_,__]]

returns True.

However,

MatchQ[1+1, Plus[_,__]]

returns False.

I believe the reason for this is that Mathematica has evaluated 1+1, and the structure of the expression has changed.

To check this:

FullForm[HoldForm[1+1]]

returns HoldForm[Plus[1,1]]

Ok. Getting closer.

Where I am stuck is passing the unevaluated expression of 1+1 to MatchQ in functional format to test.

How would I structure MatchQ to evaluate the expression 1+1 as True?

$\endgroup$
4
  • 1
    $\begingroup$ MatchQ does not have any Hold attributes. Therefore, Plus is evaluated before it is passed into MatchQ. Run the following to see the affect: Trace[MatchQ[1 + 1, Plus[_, __]]]. $\endgroup$
    – Edmund
    Sep 20, 2016 at 0:02
  • 4
    $\begingroup$ Are you looking for MatchQ[Unevaluated[1 + 1], Plus[_, __]]? $\endgroup$
    – Karsten7
    Sep 20, 2016 at 0:03
  • $\begingroup$ Also you can check out Inactive, IgnoringInactive, etc. $\endgroup$
    – QuantumDot
    Sep 20, 2016 at 0:52
  • $\begingroup$ Unevaluated was what I was looking for. Thanks. $\endgroup$
    – Todd Dixon
    Sep 20, 2016 at 13:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.