MathGroup Archive 2014

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Why does _+_==2_ (or, why is HoldPattern required for

The fundamental feature of Mathematica's programming language is
"everything is an expression".  Blank[] has the value Blank[] and only is
replaced by another value in pattern matching expressions like Replace,
Cases etc. and evaluation of replacement rules such as when a substitution
rule has been defined: f[_]:=<expression>.

Since that is not happening in this case the rule x_+x_->2 x applies, hence
the result.

On Monday, January 20, 2014, Alan <alan.isaac at> wrote:

> 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.
> Thanks,
> Alan Isaac

  • Prev by Date: Re: BSplineSurface & BSplineFunction_Extracting Data
  • Next by Date: Re: Why does _+_==2_ (or, why is HoldPattern required for
  • Previous by thread: Why does _+_==2_ (or, why is HoldPattern required for sensible matching)?
  • Next by thread: Re: Why does _+_==2_ (or, why is HoldPattern required for