About shorthand notation for conditional pattern (Paul Wellin “Essentials of Programming in Mathematica”)

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











up vote
3
down vote

favorite












I am reading Paul Wellin "Essentials of Programming in Mathematica".



Mr. Wellin wrote as follows in his above book:




"There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."




MatchQ[Range[10], s__/;NumberQ[s]]


returns error and False. And,



MatchQ[Range[10], s__?NumberQ]


returns True.



Why?










share|improve this question



























    up vote
    3
    down vote

    favorite












    I am reading Paul Wellin "Essentials of Programming in Mathematica".



    Mr. Wellin wrote as follows in his above book:




    "There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."




    MatchQ[Range[10], s__/;NumberQ[s]]


    returns error and False. And,



    MatchQ[Range[10], s__?NumberQ]


    returns True.



    Why?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am reading Paul Wellin "Essentials of Programming in Mathematica".



      Mr. Wellin wrote as follows in his above book:




      "There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."




      MatchQ[Range[10], s__/;NumberQ[s]]


      returns error and False. And,



      MatchQ[Range[10], s__?NumberQ]


      returns True.



      Why?










      share|improve this question















      I am reading Paul Wellin "Essentials of Programming in Mathematica".



      Mr. Wellin wrote as follows in his above book:




      "There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."




      MatchQ[Range[10], s__/;NumberQ[s]]


      returns error and False. And,



      MatchQ[Range[10], s__?NumberQ]


      returns True.



      Why?







      pattern-matching expression-test






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 mins ago









      J. M. is somewhat okay.♦

      94.5k10293454




      94.5k10293454










      asked 54 mins ago









      tchappy ha

      1283




      1283




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          __?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.



          The third bullet point under "Details" in the documentation for PatterTest (?) is this:




          In a form such as __?test, every element in the sequence matched by __
          must yield True when test is applied.




          MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate



          NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


          This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:



          MatchQ[Range[10], s__ /; NumberQ[s]]


          but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.



          To actually get what we want using this method, we have to do this:



          MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]


          __?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.






          share|improve this answer



























            up vote
            0
            down vote













            In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)



            What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.



            In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.



            In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.



            If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:



            MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]




            share




















            • (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
              – J. M. is somewhat okay.♦
              8 mins 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%2f183750%2fabout-shorthand-notation-for-conditional-pattern-paul-wellin-essentials-of-pro%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            __?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.



            The third bullet point under "Details" in the documentation for PatterTest (?) is this:




            In a form such as __?test, every element in the sequence matched by __
            must yield True when test is applied.




            MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate



            NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


            This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:



            MatchQ[Range[10], s__ /; NumberQ[s]]


            but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.



            To actually get what we want using this method, we have to do this:



            MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]


            __?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.






            share|improve this answer
























              up vote
              2
              down vote













              __?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.



              The third bullet point under "Details" in the documentation for PatterTest (?) is this:




              In a form such as __?test, every element in the sequence matched by __
              must yield True when test is applied.




              MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate



              NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


              This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:



              MatchQ[Range[10], s__ /; NumberQ[s]]


              but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.



              To actually get what we want using this method, we have to do this:



              MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]


              __?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                __?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.



                The third bullet point under "Details" in the documentation for PatterTest (?) is this:




                In a form such as __?test, every element in the sequence matched by __
                must yield True when test is applied.




                MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate



                NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


                This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:



                MatchQ[Range[10], s__ /; NumberQ[s]]


                but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.



                To actually get what we want using this method, we have to do this:



                MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]


                __?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.






                share|improve this answer












                __?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.



                The third bullet point under "Details" in the documentation for PatterTest (?) is this:




                In a form such as __?test, every element in the sequence matched by __
                must yield True when test is applied.




                MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate



                NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


                This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:



                MatchQ[Range[10], s__ /; NumberQ[s]]


                but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.



                To actually get what we want using this method, we have to do this:



                MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]


                __?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 19 mins ago









                C. E.

                48.1k392194




                48.1k392194




















                    up vote
                    0
                    down vote













                    In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)



                    What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.



                    In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.



                    In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.



                    If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:



                    MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]




                    share




















                    • (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                      – J. M. is somewhat okay.♦
                      8 mins ago














                    up vote
                    0
                    down vote













                    In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)



                    What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.



                    In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.



                    In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.



                    If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:



                    MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]




                    share




















                    • (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                      – J. M. is somewhat okay.♦
                      8 mins ago












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)



                    What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.



                    In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.



                    In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.



                    If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:



                    MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]




                    share












                    In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)



                    What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.



                    In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.



                    In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.



                    If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:



                    MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]





                    share











                    share


                    share










                    answered 9 mins ago









                    J. M. is somewhat okay.♦

                    94.5k10293454




                    94.5k10293454











                    • (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                      – J. M. is somewhat okay.♦
                      8 mins ago
















                    • (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                      – J. M. is somewhat okay.♦
                      8 mins ago















                    (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                    – J. M. is somewhat okay.♦
                    8 mins ago




                    (I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
                    – J. M. is somewhat okay.♦
                    8 mins 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%2f183750%2fabout-shorthand-notation-for-conditional-pattern-paul-wellin-essentials-of-pro%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    One-line joke