Re: evaluate to True?

*To*: mathgroup at smc.vnet.net*Subject*: [mg125991] Re: evaluate to True?*From*: Andrzej Kozlowski <akozlowski at gmail.com>*Date*: Wed, 11 Apr 2012 18:21:11 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <201204061001.GAA23045@smc.vnet.net> <CAEtRDSfexgBvoLTpXVPv=6B7m_CsNwqhUAWQBvnj4JMimsN-Sg@mail.gmail.com> <201204070959.FAA01206@smc.vnet.net> <C2101089-A067-4870-B787-4D02DAB46557@mimuw.edu.pl> <20120410135257.17881weume3ui42h@webmail.fundp.ac.be>

The reason why you see the same behaviour when you use MemberQ and !FreeQ is that the two are really equivalent and differ only in the default levels which they test. FreeQ by default tests all levels while MemberQ only tests level 1. But if you tell MemberQ to test all levels as follows: MemberQ[2^Sin[x], x, {0, Infinity}] True you will get exactly the same result as when you use ! FreeQ[2^Sin[x], x] True On the other hand, you can produce the effect of MemberQ by using !FreeQ and specifying level 1: ! FreeQ[2^Sin[x], x, 1] False MemberQ[2^Sin[x], x] False Andrzej Kozlowski On 10 Apr 2012, at 13:52, clhotka at fundp.ac.be wrote: > Hello, > > thank you for pointing out that MatchQ already exists (see also [mg125948]). I am suprised > that after more than 8 years of "heavy" usage of Mathematica I did never use this > function (directly). > > In fact in most of my codes I prefer to use the construct Not[FreeQ[#]]& instead > of MemberQ. It is interesting to note that it still has the same drawback as MemberQ: > > LiteralMemberQ[expr_, form_] := MemberQ[expr, Verbatim[form]] > myMemberQ[expr_, form_] := Not[FreeQ[expr, form]] > > LiteralMemberQ[{0, 1, 2}, 2] > True > > myMemberQ[{0, 1, 2}, 2] > True > > LiteralMemberQ[{0, 1, 2}, expr_] > False > > myMemberQ[{0, 1, 2}, expr_] > True > > LiteralMemberQ[{0, 1, expr_}, expr_] > True > > myMemberQ[{0, 1, expr_}, expr_] > True > > As you can see the difference in the above examples is that LiteralMemberQ[{0,1,2},expr_] returns False while myMemberQ[{0,1,2},expr_] returns True. > > > I am still thinking that both functions should return neither True nor False (see argumentation below) in this case. It took me some time to design the following > function: > > > myMemberQ[expr_, form_] := Switch[Head[form], > Pattern, > If[Intersection[ > Cases[expr, Verbatim[form], \[Infinity]], {form}] != {}, True, > Undefined], > _, > Not[FreeQ[expr, form]]] > > The behaviour is now: > > myMemberQ[{0, 1, 2}, 3] > False > > myMemberQ[{0, 1, 2}, 2] > True > > myMemberQ[{0, 1, 2}, expr_] > Undefined > > myMemberQ[{0, 1, expr_}, expr_] > True > > with the additional nice property that > > myMemberQ[{0, 1, 2^expr_}, expr_] > True > > LiteralMemberQ[{0, 1, 2^expr_}, expr_] > False > > while > > MemberQ[{0, 1, 2^expr_}, expr_] > > returns > > True > > not because of expr_ in 2^expr_ but because every element of {0,1,2^expr_} matches expr_. > > Please let me know your opinion, > > Christoph > > > > Quoting Andrzej Kozlowski <akozlowski at gmail.com>: > >> Of course there is already a function called "MatchQ". Second, MemberQ can actually be used for "literal" selection of the kind you seem to be thinking about. >> >> For example: >> >> MemberQ[{0, 1, 2}, expr_] >> >> True >> >> but >> >> MemberQ[{0, 1, 2}, Verbatim[expr_]] >> >> False >> >> MemberQ[{0, 1, expr_}, Verbatim[expr_]] >> >> True >> >> Thus we can define a "literal" membership checking function as follows: >> >> LiteralMemberQ[expr_, form_] := MemberQ[expr, Verbatim[form]] >> >> Now it behaves the way you expected: >> >> LiteralMemberQ[{0, 1, 2}, 2] >> >> True >> >> LiteralMemberQ[{0, 1, 2}, expr_] >> >> False >> >> and even: >> >> LiteralMemberQ[{0, 1, expr_}, expr_] >> >> True >> >> Andrzej Kozlowski >> >> >> On 7 Apr 2012, at 11:59, Christoph Lhotka wrote: >> >>> Hello, >>> >>> yes you are right, I mean MemberQ rather than ModuleQ (please see my >>> correction of the post [mg125913]). >>> >>> In fact the behaviour is consistent with the information you get for >>> MemberQ: >>> >>> In[1]:= ?MemberQ >>> >>> "MemberQ[list, form] returns True if an element of list matches form, >>> and False otherwise." >>> >>> In fact the function name is misleading (at least to me): form is never >>> a member of list if MemberQ >>> returns True. If this would be the case my argumentation (below, >>> original post) would bring the >>> behaviour of the function in troubles if form is the "expression for >>> everything". >>> >>> The misinterpretation of the function due to the name can be the cause >>> of severe bugs as seen >>> in message [mg125911]. Maybe a name like MatchQ would be more >>> appropriate for future versions >>> of Mathematica. >>> >>> Best, >>> >>> Christoph >>> >>> >>> >>> >>> On 04/06/2012 02:38 PM, Bob Hanlon wrote: >>>> You mean MemberQ rather than ModuleQ. In MemberQ[list, expr_] a blank >>>> (with or without a name for the blank) matches anything. >>>> >>>> {MemberQ[{a}, _], >>>> MemberQ[{"a"}, _], >>>> MemberQ[{Indeterminate}, _], >>>> MemberQ[{ComplexInfinity}, _], >>>> MemberQ[{Plot[x, {x, 0, 1}]}, _]} >>>> >>>> {True, True, True, True, True} >>>> >>>> >>>> Bob Hanlon >>>> >>>> On Fri, Apr 6, 2012 at 6:01 AM, Christoph Lhotka >>>> <christoph.lhotka at fundp.ac.be> wrote: >>>>> Hello, >>>>> >>>>> I found and interesting subject of discussion in the post >>>>> >>>>> "Bug in pattern test, or I did something wrong?" >>>>> >>>>> >>>>> I could trace back the problem to an issue with ModuleQ. >>>>> >>>>> Question: Why does >>>>> >>>>> In[12]:= ModuleQ[{0,1,2},expr_] >>>>> >>>>> Out[12]:= True >>>>> >>>>> evaluate to True? >>>>> >>>>> >>>>> My argumentation is as follows: >>>>> >>>>> On the one hand there could be a chance that expr_ is 0,1 or 2 but on >>>>> the other >>>>> hand the probability that expr_ is not 0,1 or 2 is even higher. As a >>>>> conclusion it should neither >>>>> evaluate to True nor to False. >>>>> >>>>> In other words: Is there any reason why the expression of everything >>>>> (named expr) >>>>> is contained in the set {0,1,2} ? >>>>> >>>>> Best, >>>>> >>>>> Christoph >>>>> >>>> >>>> >>> >>> >> >> > >

**References**:**Why does ModuleQ[{0,1,2}, expr_] evaluate to True?***From:*Christoph Lhotka <christoph.lhotka@fundp.ac.be>

**Re: evaluate to True?***From:*Christoph Lhotka <christoph.lhotka@fundp.ac.be>

**Re: fyi, small note on using Mathematica for object based programming**

**Re: evaluate to True?**

**Re: evaluate to True?**

**Re: evaluate to True?**