Different behaviours of Cases









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]]
(**)






functions pattern-matching






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 at 14:11









kglr

173k8194400




173k8194400










asked Aug 23 at 8:32









Giancarlo

452311




452311







  • 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: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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















      Required, but never shown

























      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

          173k8194400




          173k8194400




















              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

              452311




              452311











              • 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

                  45.6k466132




                  45.6k466132



























                       

                      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















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                      Edmonton

                      Crossroads (UK TV series)