Why does MatchQ[a, r_ /; Head[r] != Plus] evaluate to False?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I'm trying to understand. Why is it that:



MatchQ[a, r_ /; Head[r] != Plus]


Evaluates to:



False


? For me, I would think that, because:



Head[a]


Evaluates to:



Symbol


Where a has no value or expression assigned to it, then this:



MatchQ[a, r_ /; Head[r] != Plus]


Should evaluate to True. Could someone point me in the right direction to understanding this better? Thanks!










share|improve this question

























    up vote
    3
    down vote

    favorite












    I'm trying to understand. Why is it that:



    MatchQ[a, r_ /; Head[r] != Plus]


    Evaluates to:



    False


    ? For me, I would think that, because:



    Head[a]


    Evaluates to:



    Symbol


    Where a has no value or expression assigned to it, then this:



    MatchQ[a, r_ /; Head[r] != Plus]


    Should evaluate to True. Could someone point me in the right direction to understanding this better? Thanks!










    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm trying to understand. Why is it that:



      MatchQ[a, r_ /; Head[r] != Plus]


      Evaluates to:



      False


      ? For me, I would think that, because:



      Head[a]


      Evaluates to:



      Symbol


      Where a has no value or expression assigned to it, then this:



      MatchQ[a, r_ /; Head[r] != Plus]


      Should evaluate to True. Could someone point me in the right direction to understanding this better? Thanks!










      share|improve this question













      I'm trying to understand. Why is it that:



      MatchQ[a, r_ /; Head[r] != Plus]


      Evaluates to:



      False


      ? For me, I would think that, because:



      Head[a]


      Evaluates to:



      Symbol


      Where a has no value or expression assigned to it, then this:



      MatchQ[a, r_ /; Head[r] != Plus]


      Should evaluate to True. Could someone point me in the right direction to understanding this better? Thanks!







      pattern-matching head






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Jmeeks29ig

      39518




      39518




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          The problem is this:



          If a is a symbol then its Head is Symbol, so Head[a] =!= Plus reduces to Symbol != Plus. Unequal (!=) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus should evaluate to True or to False, since both sides are Symbols. Here an example why this is undecidable with the current amount of information:



          With[Symbol = 1, Plus = 1, Symbol != Plus]
          With[Symbol = 1, Plus = 0, Symbol != Plus]



          False



          True




          So the expression Symbol != Plus stays unevaluated. (This is the best strategy since later definitions could make it decidable.)



          Because the second argument of Condition (/;) does not evaluate to True, the pattern does not match.



          Lesson to learn: For testing for structual inequality, use UnsameQ (=!=):



          MatchQ[a, r_ /; Head[r] =!= Plus]



          True




          Of course, the same applies, mutatis mutandis, to Equal and SameQ.






          share|improve this answer






















          • Awesome, thanks! That helps a lot!
            – Jmeeks29ig
            1 hour ago










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182059%2fwhy-does-matchqa-r-headr-plus-evaluate-to-false%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          The problem is this:



          If a is a symbol then its Head is Symbol, so Head[a] =!= Plus reduces to Symbol != Plus. Unequal (!=) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus should evaluate to True or to False, since both sides are Symbols. Here an example why this is undecidable with the current amount of information:



          With[Symbol = 1, Plus = 1, Symbol != Plus]
          With[Symbol = 1, Plus = 0, Symbol != Plus]



          False



          True




          So the expression Symbol != Plus stays unevaluated. (This is the best strategy since later definitions could make it decidable.)



          Because the second argument of Condition (/;) does not evaluate to True, the pattern does not match.



          Lesson to learn: For testing for structual inequality, use UnsameQ (=!=):



          MatchQ[a, r_ /; Head[r] =!= Plus]



          True




          Of course, the same applies, mutatis mutandis, to Equal and SameQ.






          share|improve this answer






















          • Awesome, thanks! That helps a lot!
            – Jmeeks29ig
            1 hour ago














          up vote
          5
          down vote



          accepted










          The problem is this:



          If a is a symbol then its Head is Symbol, so Head[a] =!= Plus reduces to Symbol != Plus. Unequal (!=) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus should evaluate to True or to False, since both sides are Symbols. Here an example why this is undecidable with the current amount of information:



          With[Symbol = 1, Plus = 1, Symbol != Plus]
          With[Symbol = 1, Plus = 0, Symbol != Plus]



          False



          True




          So the expression Symbol != Plus stays unevaluated. (This is the best strategy since later definitions could make it decidable.)



          Because the second argument of Condition (/;) does not evaluate to True, the pattern does not match.



          Lesson to learn: For testing for structual inequality, use UnsameQ (=!=):



          MatchQ[a, r_ /; Head[r] =!= Plus]



          True




          Of course, the same applies, mutatis mutandis, to Equal and SameQ.






          share|improve this answer






















          • Awesome, thanks! That helps a lot!
            – Jmeeks29ig
            1 hour ago












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          The problem is this:



          If a is a symbol then its Head is Symbol, so Head[a] =!= Plus reduces to Symbol != Plus. Unequal (!=) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus should evaluate to True or to False, since both sides are Symbols. Here an example why this is undecidable with the current amount of information:



          With[Symbol = 1, Plus = 1, Symbol != Plus]
          With[Symbol = 1, Plus = 0, Symbol != Plus]



          False



          True




          So the expression Symbol != Plus stays unevaluated. (This is the best strategy since later definitions could make it decidable.)



          Because the second argument of Condition (/;) does not evaluate to True, the pattern does not match.



          Lesson to learn: For testing for structual inequality, use UnsameQ (=!=):



          MatchQ[a, r_ /; Head[r] =!= Plus]



          True




          Of course, the same applies, mutatis mutandis, to Equal and SameQ.






          share|improve this answer














          The problem is this:



          If a is a symbol then its Head is Symbol, so Head[a] =!= Plus reduces to Symbol != Plus. Unequal (!=) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus should evaluate to True or to False, since both sides are Symbols. Here an example why this is undecidable with the current amount of information:



          With[Symbol = 1, Plus = 1, Symbol != Plus]
          With[Symbol = 1, Plus = 0, Symbol != Plus]



          False



          True




          So the expression Symbol != Plus stays unevaluated. (This is the best strategy since later definitions could make it decidable.)



          Because the second argument of Condition (/;) does not evaluate to True, the pattern does not match.



          Lesson to learn: For testing for structual inequality, use UnsameQ (=!=):



          MatchQ[a, r_ /; Head[r] =!= Plus]



          True




          Of course, the same applies, mutatis mutandis, to Equal and SameQ.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Henrik Schumacher

          38.4k251112




          38.4k251112











          • Awesome, thanks! That helps a lot!
            – Jmeeks29ig
            1 hour ago
















          • Awesome, thanks! That helps a lot!
            – Jmeeks29ig
            1 hour ago















          Awesome, thanks! That helps a lot!
          – Jmeeks29ig
          1 hour ago




          Awesome, thanks! That helps a lot!
          – Jmeeks29ig
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182059%2fwhy-does-matchqa-r-headr-plus-evaluate-to-false%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery