Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Why does _+_==2_ (or, why is HoldPattern required for sensible matching)?
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  

Posts: 95
Registered: 3/29/11
Why does _+_==2_ (or, why is HoldPattern required for sensible matching)?
Posted: Jan 20, 2014 3:53 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

I'm a relatively new user of Mathematica, and the following behavior seems odd to me:

In[1]:= _ + _
Out[1]= 2 _

Use of `Plus` here is just to illustrate a more general "problem with `Blank`. I had expected pattern objects to resist such evaluation. This is possibly related to the fact that I was surprised by the following:

In[2]:= Blank[] == Blank[]
Out[2]= True

Since each Blank[] can match anything, I find this conceptually to be the wrong behavior. As an example of a counter-intuitive result:

In[3]:= MatchQ[a + b, _ + _]
Out[3]= False

I understand that I can deal with this problem by using HoldPattern.

In[4]:= MatchQ[a + b, HoldPattern[_ + _]]
Out[4]= True

So my question is not how to deal with the problem, but rather is a request for an explanation of why the current pattern evaluation behavior is desirable.

Alan Isaac

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2016. All Rights Reserved.