Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16453

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

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Why does _+_==2_ (or, why is HoldPattern required for
Date 2014-01-21 07:58 +0000
Message-ID <lbl9b2$lsq$1@smc.vnet.net> (permalink)
References <20140120085945.0A76D69CD@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


Since MatchQ does NOT have the attribute HoldAll or HoldRest, the second
argument is evaluated before the test and without at least one name
associated with the blanks to make them distinct you get 2_


Attributes[{MatchQ, Cases}]


{{Protected}, {Protected}}


list = {a, b + c, d, e + f, 2 g};


r1 = Select[list, MatchQ[#, x_ + y_] &]


{b + c, e + f}


r1 ==
 Select[list, MatchQ[#, x_ + _] &] ==
 Select[list, MatchQ[#, _ + y_] &] ==
 Cases[list, x_ + y_] ==
 Cases[list, x_ + _] ==
 Cases[list, _ + y_]


True


r2 = Select[list, MatchQ[#, _ + _] &]


{2 g}


r2 == Cases[list, _ + _]


True


Although counter-intuitive, it is consistent and hence predictable.



Bob Hanlon




On Mon, Jan 20, 2014 at 3:59 AM, Alan <alan.isaac@gmail.com> 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
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Why does _+_==2_ (or, why is HoldPattern required for Bob Hanlon <hanlonr357@gmail.com> - 2014-01-21 07:58 +0000

csiph-web