Is there a defined priority for pattern matching?Using a PatternTest versus a Condition for pattern...

Cat is tipping over bed-side lamps during the night

Saint abbreviation

Why does magnet wire need to be insulated?

What game did these black and yellow dice come from?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Short story where statues have their heads replaced by those of carved insect heads

How does one write from a minority culture? A question on cultural references

What is a good reason for every spaceship to carry a weapon on board?

Why did Luke use his left hand to shoot?

Is using an 'empty' metaphor considered bad style?

Airplane generations - how does it work?

Hilchos Shabbos English Sefer

How do I prevent a homebrew Grappling Hook feature from trivializing Tomb of Annihilation?

Why is Agricola named as such?

Has Britain negotiated with any other countries outside the EU in preparation for the exit?

What will happen if Parliament votes "no" on each of the Brexit-related votes to be held on the 12th, 13th and 14th of March?

TikZ graph edges not drawn nicely

What happened to my GE option?

How do you voice extended chords?

What is a DAG (Graph Theory)?

Why does PHOTOREC keep finding files?

Why is it that Bernie Sanders is always called a "socialist"?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

After checking in online, how do I know whether I need to go show my passport at airport check-in?



Is there a defined priority for pattern matching?


Using a PatternTest versus a Condition for pattern matchingIs pure pattern matching without PatternTest and Condition Turing-complete?Classifying poker hands by pattern matchingMore general pattern fails to match everything the more specific pattern does.Detecting a more general patternPattern matching to head with HoldFirstHow to visualize pattern matching process?Pattern matching an expression involving TimesCompare arrays using custom pattern matchingPattern Match Bug with Equals?













3












$begingroup$


If you define a function g, like so:



g[x_?EvenQ] := "even"    
g[x_?NumberQ] := "number"


why will Mathematica always return g[2] == "Even", even though NumberQ[2] == True?



More specifically, is there a defined order in which Mathematica will try to match function cases? Does it evaluate the most specific match first?










share|improve this question









New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    That's right---fixed.
    $endgroup$
    – wgoodall01
    1 hour ago






  • 2




    $begingroup$
    Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    Or just execute ?g to see the order of the definitions.
    $endgroup$
    – Roman
    16 mins ago
















3












$begingroup$


If you define a function g, like so:



g[x_?EvenQ] := "even"    
g[x_?NumberQ] := "number"


why will Mathematica always return g[2] == "Even", even though NumberQ[2] == True?



More specifically, is there a defined order in which Mathematica will try to match function cases? Does it evaluate the most specific match first?










share|improve this question









New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    That's right---fixed.
    $endgroup$
    – wgoodall01
    1 hour ago






  • 2




    $begingroup$
    Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    Or just execute ?g to see the order of the definitions.
    $endgroup$
    – Roman
    16 mins ago














3












3








3





$begingroup$


If you define a function g, like so:



g[x_?EvenQ] := "even"    
g[x_?NumberQ] := "number"


why will Mathematica always return g[2] == "Even", even though NumberQ[2] == True?



More specifically, is there a defined order in which Mathematica will try to match function cases? Does it evaluate the most specific match first?










share|improve this question









New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




If you define a function g, like so:



g[x_?EvenQ] := "even"    
g[x_?NumberQ] := "number"


why will Mathematica always return g[2] == "Even", even though NumberQ[2] == True?



More specifically, is there a defined order in which Mathematica will try to match function cases? Does it evaluate the most specific match first?







pattern-matching expression-test






share|improve this question









New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago







wgoodall01













New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









wgoodall01wgoodall01

1185




1185




New contributor




wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






wgoodall01 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    That's right---fixed.
    $endgroup$
    – wgoodall01
    1 hour ago






  • 2




    $begingroup$
    Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    Or just execute ?g to see the order of the definitions.
    $endgroup$
    – Roman
    16 mins ago


















  • $begingroup$
    g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    That's right---fixed.
    $endgroup$
    – wgoodall01
    1 hour ago






  • 2




    $begingroup$
    Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
    $endgroup$
    – J. M. is computer-less
    1 hour ago












  • $begingroup$
    Or just execute ?g to see the order of the definitions.
    $endgroup$
    – Roman
    16 mins ago
















$begingroup$
g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
$endgroup$
– J. M. is computer-less
1 hour ago






$begingroup$
g[x_NumberQ] means to match arguments with the head NumberQ. What you want is to match arguments such that NumberQ[x] is True, which is g[x_?NumberQ]. Look up PatternTest[]. (Before you make this correction, make sure to run Clear[g] first.)
$endgroup$
– J. M. is computer-less
1 hour ago














$begingroup$
That's right---fixed.
$endgroup$
– wgoodall01
1 hour ago




$begingroup$
That's right---fixed.
$endgroup$
– wgoodall01
1 hour ago




2




2




$begingroup$
Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
$endgroup$
– J. M. is computer-less
1 hour ago






$begingroup$
Once you have made this fix, evaluate DownValues[g]; that should give a hint as to which case gets applied first. Generally, it tries to put special cases first before general ones.
$endgroup$
– J. M. is computer-less
1 hour ago














$begingroup$
Or just execute ?g to see the order of the definitions.
$endgroup$
– Roman
16 mins ago




$begingroup$
Or just execute ?g to see the order of the definitions.
$endgroup$
– Roman
16 mins ago










1 Answer
1






active

oldest

votes


















4












$begingroup$

TheOrderingOfDefinitions




  • ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried before more general cases.

  • Although in many practical cases, the Wolfram System can recognize when one rule is more general than another, you should realize that this is not always possible. For example, if two rules both contain complicated /; conditions, it may not be possible to work out which is more general, and, in fact, there may not be a definite ordering. Whenever the appropriate ordering is not clear, the Wolfram System stores rules in the order you give them.




ClearAll[g]
g[x_?EvenQ] := "even"
g[x_?NumberQ] := "number"
g /@ {1, 2}



{"number", "even"}




ClearAll[h]
h[x_?NumberQ] := "number"
h[x_?EvenQ] := "even"
h /@ {1, 2}



{"number", "number"}







share|improve this answer











$endgroup$













    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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });






    wgoodall01 is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192278%2fis-there-a-defined-priority-for-pattern-matching%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4












    $begingroup$

    TheOrderingOfDefinitions




    • ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried before more general cases.

    • Although in many practical cases, the Wolfram System can recognize when one rule is more general than another, you should realize that this is not always possible. For example, if two rules both contain complicated /; conditions, it may not be possible to work out which is more general, and, in fact, there may not be a definite ordering. Whenever the appropriate ordering is not clear, the Wolfram System stores rules in the order you give them.




    ClearAll[g]
    g[x_?EvenQ] := "even"
    g[x_?NumberQ] := "number"
    g /@ {1, 2}



    {"number", "even"}




    ClearAll[h]
    h[x_?NumberQ] := "number"
    h[x_?EvenQ] := "even"
    h /@ {1, 2}



    {"number", "number"}







    share|improve this answer











    $endgroup$


















      4












      $begingroup$

      TheOrderingOfDefinitions




      • ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried before more general cases.

      • Although in many practical cases, the Wolfram System can recognize when one rule is more general than another, you should realize that this is not always possible. For example, if two rules both contain complicated /; conditions, it may not be possible to work out which is more general, and, in fact, there may not be a definite ordering. Whenever the appropriate ordering is not clear, the Wolfram System stores rules in the order you give them.




      ClearAll[g]
      g[x_?EvenQ] := "even"
      g[x_?NumberQ] := "number"
      g /@ {1, 2}



      {"number", "even"}




      ClearAll[h]
      h[x_?NumberQ] := "number"
      h[x_?EvenQ] := "even"
      h /@ {1, 2}



      {"number", "number"}







      share|improve this answer











      $endgroup$
















        4












        4








        4





        $begingroup$

        TheOrderingOfDefinitions




        • ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried before more general cases.

        • Although in many practical cases, the Wolfram System can recognize when one rule is more general than another, you should realize that this is not always possible. For example, if two rules both contain complicated /; conditions, it may not be possible to work out which is more general, and, in fact, there may not be a definite ordering. Whenever the appropriate ordering is not clear, the Wolfram System stores rules in the order you give them.




        ClearAll[g]
        g[x_?EvenQ] := "even"
        g[x_?NumberQ] := "number"
        g /@ {1, 2}



        {"number", "even"}




        ClearAll[h]
        h[x_?NumberQ] := "number"
        h[x_?EvenQ] := "even"
        h /@ {1, 2}



        {"number", "number"}







        share|improve this answer











        $endgroup$



        TheOrderingOfDefinitions




        • ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried before more general cases.

        • Although in many practical cases, the Wolfram System can recognize when one rule is more general than another, you should realize that this is not always possible. For example, if two rules both contain complicated /; conditions, it may not be possible to work out which is more general, and, in fact, there may not be a definite ordering. Whenever the appropriate ordering is not clear, the Wolfram System stores rules in the order you give them.




        ClearAll[g]
        g[x_?EvenQ] := "even"
        g[x_?NumberQ] := "number"
        g /@ {1, 2}



        {"number", "even"}




        ClearAll[h]
        h[x_?NumberQ] := "number"
        h[x_?EvenQ] := "even"
        h /@ {1, 2}



        {"number", "number"}








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        kglrkglr

        186k10202421




        186k10202421






















            wgoodall01 is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            wgoodall01 is a new contributor. Be nice, and check out our Code of Conduct.













            wgoodall01 is a new contributor. Be nice, and check out our Code of Conduct.












            wgoodall01 is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192278%2fis-there-a-defined-priority-for-pattern-matching%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

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            Meter-Bus Содержание Параметры шины | Стандартизация |...