Different behaviours of Cases

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











up vote
9
down vote

favorite












Why does Cases output an element only in the first of the following lines?



Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> π b, HoldPattern[a -> π b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)






share|improve this question


















  • 3




    compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
    – Kuba♦
    Aug 23 at 8:38











  • Huh, that was a nasty one!
    – Henrik Schumacher
    Aug 23 at 8:58






  • 2




    Use PatternSequence in place of HoldPattern
    – kglr
    Aug 23 at 8:59










  • @kglr I think that is worth an answer and an explanation. (I'm personally interested.)
    – Henrik Schumacher
    Aug 23 at 9:01






  • 1




    See also this: mathematica.stackexchange.com/a/73020/5478
    – Kuba♦
    Aug 23 at 9:12














up vote
9
down vote

favorite












Why does Cases output an element only in the first of the following lines?



Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> π b, HoldPattern[a -> π b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)






share|improve this question


















  • 3




    compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
    – Kuba♦
    Aug 23 at 8:38











  • Huh, that was a nasty one!
    – Henrik Schumacher
    Aug 23 at 8:58






  • 2




    Use PatternSequence in place of HoldPattern
    – kglr
    Aug 23 at 8:59










  • @kglr I think that is worth an answer and an explanation. (I'm personally interested.)
    – Henrik Schumacher
    Aug 23 at 9:01






  • 1




    See also this: mathematica.stackexchange.com/a/73020/5478
    – Kuba♦
    Aug 23 at 9:12












up vote
9
down vote

favorite









up vote
9
down vote

favorite











Why does Cases output an element only in the first of the following lines?



Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> π b, HoldPattern[a -> π b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)






share|improve this question














Why does Cases output an element only in the first of the following lines?



Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> π b, HoldPattern[a -> π b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)








share|improve this question













share|improve this question




share|improve this question








edited Aug 23 at 14:11









kglr

157k8182379




157k8182379










asked Aug 23 at 8:32









Giancarlo

450211




450211







  • 3




    compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
    – Kuba♦
    Aug 23 at 8:38











  • Huh, that was a nasty one!
    – Henrik Schumacher
    Aug 23 at 8:58






  • 2




    Use PatternSequence in place of HoldPattern
    – kglr
    Aug 23 at 8:59










  • @kglr I think that is worth an answer and an explanation. (I'm personally interested.)
    – Henrik Schumacher
    Aug 23 at 9:01






  • 1




    See also this: mathematica.stackexchange.com/a/73020/5478
    – Kuba♦
    Aug 23 at 9:12












  • 3




    compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
    – Kuba♦
    Aug 23 at 8:38











  • Huh, that was a nasty one!
    – Henrik Schumacher
    Aug 23 at 8:58






  • 2




    Use PatternSequence in place of HoldPattern
    – kglr
    Aug 23 at 8:59










  • @kglr I think that is worth an answer and an explanation. (I'm personally interested.)
    – Henrik Schumacher
    Aug 23 at 9:01






  • 1




    See also this: mathematica.stackexchange.com/a/73020/5478
    – Kuba♦
    Aug 23 at 9:12







3




3




compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
– Kuba♦
Aug 23 at 8:38





compare FullForm@HoldPattern[a -> 1/2 b] and FullForm@1, a -> 1/2 b, the rest is about reordering.
– Kuba♦
Aug 23 at 8:38













Huh, that was a nasty one!
– Henrik Schumacher
Aug 23 at 8:58




Huh, that was a nasty one!
– Henrik Schumacher
Aug 23 at 8:58




2




2




Use PatternSequence in place of HoldPattern
– kglr
Aug 23 at 8:59




Use PatternSequence in place of HoldPattern
– kglr
Aug 23 at 8:59












@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
– Henrik Schumacher
Aug 23 at 9:01




@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
– Henrik Schumacher
Aug 23 at 9:01




1




1




See also this: mathematica.stackexchange.com/a/73020/5478
– Kuba♦
Aug 23 at 9:12




See also this: mathematica.stackexchange.com/a/73020/5478
– Kuba♦
Aug 23 at 9:12










3 Answers
3






active

oldest

votes

















up vote
8
down vote



accepted










As an alternative to HoldPattern[Evaluate[...] you can use PatternSequence which evaluates its argument:



Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> π b, PatternSequence[a -> π b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]



a -> 2 b, a -> b/2, a -> b π, a -> b c




Alternatively, give the pattern a name:



Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> π b, p : (a -> π b)],
Cases[1, a -> c b, p : (a -> c b)]



a -> 2 b, a -> b/2, a -> b π, a -> b c







share|improve this answer





























    up vote
    5
    down vote













    Thanks to Kuba's comment now I see:
    HoldPattern prevents the evaluation of its argument, so Cases don't match the element in the list a->b/2 that is



    Times[Rational[1, 2], b]


    with the unevaluated pattern



    Times[b, Power[2, -1]]


    Evaluate the argument of HoldPattern solves the problem:



    Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
    (*a -> b/2*)


    Thanks Kuba!






    share|improve this answer




















    • You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
      – kkm
      Aug 23 at 9:19


















    up vote
    2
    down vote













    Here is another way by using Verbatim. Maybe it feels a bit less hacky.




    Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
    Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
    Cases[1, a -> π b, Verbatim[a -> π b]],
    Cases[1, a -> c b, Verbatim[a -> c b]]




    a -> 2 b, a -> b/2, a -> b π, a -> b c







    share|improve this answer




















      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%2f180489%2fdifferent-behaviours-of-cases%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      8
      down vote



      accepted










      As an alternative to HoldPattern[Evaluate[...] you can use PatternSequence which evaluates its argument:



      Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
      Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
      Cases[1, a -> π b, PatternSequence[a -> π b]],
      Cases[1, a -> c b, PatternSequence[a -> c b]]



      a -> 2 b, a -> b/2, a -> b π, a -> b c




      Alternatively, give the pattern a name:



      Cases[1, a -> 2 b, p : (a -> 2 b)],
      Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
      Cases[1, a -> π b, p : (a -> π b)],
      Cases[1, a -> c b, p : (a -> c b)]



      a -> 2 b, a -> b/2, a -> b π, a -> b c







      share|improve this answer


























        up vote
        8
        down vote



        accepted










        As an alternative to HoldPattern[Evaluate[...] you can use PatternSequence which evaluates its argument:



        Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
        Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
        Cases[1, a -> π b, PatternSequence[a -> π b]],
        Cases[1, a -> c b, PatternSequence[a -> c b]]



        a -> 2 b, a -> b/2, a -> b π, a -> b c




        Alternatively, give the pattern a name:



        Cases[1, a -> 2 b, p : (a -> 2 b)],
        Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
        Cases[1, a -> π b, p : (a -> π b)],
        Cases[1, a -> c b, p : (a -> c b)]



        a -> 2 b, a -> b/2, a -> b π, a -> b c







        share|improve this answer
























          up vote
          8
          down vote



          accepted







          up vote
          8
          down vote



          accepted






          As an alternative to HoldPattern[Evaluate[...] you can use PatternSequence which evaluates its argument:



          Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
          Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
          Cases[1, a -> π b, PatternSequence[a -> π b]],
          Cases[1, a -> c b, PatternSequence[a -> c b]]



          a -> 2 b, a -> b/2, a -> b π, a -> b c




          Alternatively, give the pattern a name:



          Cases[1, a -> 2 b, p : (a -> 2 b)],
          Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
          Cases[1, a -> π b, p : (a -> π b)],
          Cases[1, a -> c b, p : (a -> c b)]



          a -> 2 b, a -> b/2, a -> b π, a -> b c







          share|improve this answer














          As an alternative to HoldPattern[Evaluate[...] you can use PatternSequence which evaluates its argument:



          Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
          Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
          Cases[1, a -> π b, PatternSequence[a -> π b]],
          Cases[1, a -> c b, PatternSequence[a -> c b]]



          a -> 2 b, a -> b/2, a -> b π, a -> b c




          Alternatively, give the pattern a name:



          Cases[1, a -> 2 b, p : (a -> 2 b)],
          Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
          Cases[1, a -> π b, p : (a -> π b)],
          Cases[1, a -> c b, p : (a -> c b)]



          a -> 2 b, a -> b/2, a -> b π, a -> b c








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 23 at 9:34

























          answered Aug 23 at 9:28









          kglr

          157k8182379




          157k8182379




















              up vote
              5
              down vote













              Thanks to Kuba's comment now I see:
              HoldPattern prevents the evaluation of its argument, so Cases don't match the element in the list a->b/2 that is



              Times[Rational[1, 2], b]


              with the unevaluated pattern



              Times[b, Power[2, -1]]


              Evaluate the argument of HoldPattern solves the problem:



              Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
              (*a -> b/2*)


              Thanks Kuba!






              share|improve this answer




















              • You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
                – kkm
                Aug 23 at 9:19















              up vote
              5
              down vote













              Thanks to Kuba's comment now I see:
              HoldPattern prevents the evaluation of its argument, so Cases don't match the element in the list a->b/2 that is



              Times[Rational[1, 2], b]


              with the unevaluated pattern



              Times[b, Power[2, -1]]


              Evaluate the argument of HoldPattern solves the problem:



              Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
              (*a -> b/2*)


              Thanks Kuba!






              share|improve this answer




















              • You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
                – kkm
                Aug 23 at 9:19













              up vote
              5
              down vote










              up vote
              5
              down vote









              Thanks to Kuba's comment now I see:
              HoldPattern prevents the evaluation of its argument, so Cases don't match the element in the list a->b/2 that is



              Times[Rational[1, 2], b]


              with the unevaluated pattern



              Times[b, Power[2, -1]]


              Evaluate the argument of HoldPattern solves the problem:



              Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
              (*a -> b/2*)


              Thanks Kuba!






              share|improve this answer












              Thanks to Kuba's comment now I see:
              HoldPattern prevents the evaluation of its argument, so Cases don't match the element in the list a->b/2 that is



              Times[Rational[1, 2], b]


              with the unevaluated pattern



              Times[b, Power[2, -1]]


              Evaluate the argument of HoldPattern solves the problem:



              Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
              (*a -> b/2*)


              Thanks Kuba!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 23 at 9:01









              Giancarlo

              450211




              450211











              • You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
                – kkm
                Aug 23 at 9:19

















              • You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
                – kkm
                Aug 23 at 9:19
















              You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
              – kkm
              Aug 23 at 9:19





              You can define myHoldPattern[x___] = HoldPattern[x]; and use in place of HoldPattern to reduce clutter. Since myHoldPattern does not hold its argument, unlike the bona fide HoldPattern, the evaluation will have happened by the time x is wrapped into HoldPattern for matching.
              – kkm
              Aug 23 at 9:19











              up vote
              2
              down vote













              Here is another way by using Verbatim. Maybe it feels a bit less hacky.




              Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
              Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
              Cases[1, a -> π b, Verbatim[a -> π b]],
              Cases[1, a -> c b, Verbatim[a -> c b]]




              a -> 2 b, a -> b/2, a -> b π, a -> b c







              share|improve this answer
























                up vote
                2
                down vote













                Here is another way by using Verbatim. Maybe it feels a bit less hacky.




                Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
                Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
                Cases[1, a -> π b, Verbatim[a -> π b]],
                Cases[1, a -> c b, Verbatim[a -> c b]]




                a -> 2 b, a -> b/2, a -> b π, a -> b c







                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Here is another way by using Verbatim. Maybe it feels a bit less hacky.




                  Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
                  Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
                  Cases[1, a -> π b, Verbatim[a -> π b]],
                  Cases[1, a -> c b, Verbatim[a -> c b]]




                  a -> 2 b, a -> b/2, a -> b π, a -> b c







                  share|improve this answer












                  Here is another way by using Verbatim. Maybe it feels a bit less hacky.




                  Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
                  Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
                  Cases[1, a -> π b, Verbatim[a -> π b]],
                  Cases[1, a -> c b, Verbatim[a -> c b]]




                  a -> 2 b, a -> b/2, a -> b π, a -> b c








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 24 at 19:13









                  Henrik Schumacher

                  35.9k249102




                  35.9k249102



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180489%2fdifferent-behaviours-of-cases%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      這個網誌中的熱門文章

                      How to combine Bézier curves to a surface?

                      Mutual Information Always Non-negative

                      Why am i infinitely getting the same tweet with the Twitter Search API?