Pattern matching list of lists containing only numbers

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











up vote
4
down vote

favorite












I'm trying to define a pattern that will match lists of lists, where the inner lists contains only numbers (I want the sum of each list). Like this



1, 2, 3, 4


But not like this



1, 1, 3,3


Why this is not working?



Clear[foo];
foo[x : v___List] := Plus @@@ x /; VectorQ[v, NumberQ]


Thank you!







share|improve this question




















  • your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
    – kglr
    Aug 21 at 20:00















up vote
4
down vote

favorite












I'm trying to define a pattern that will match lists of lists, where the inner lists contains only numbers (I want the sum of each list). Like this



1, 2, 3, 4


But not like this



1, 1, 3,3


Why this is not working?



Clear[foo];
foo[x : v___List] := Plus @@@ x /; VectorQ[v, NumberQ]


Thank you!







share|improve this question




















  • your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
    – kglr
    Aug 21 at 20:00













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm trying to define a pattern that will match lists of lists, where the inner lists contains only numbers (I want the sum of each list). Like this



1, 2, 3, 4


But not like this



1, 1, 3,3


Why this is not working?



Clear[foo];
foo[x : v___List] := Plus @@@ x /; VectorQ[v, NumberQ]


Thank you!







share|improve this question












I'm trying to define a pattern that will match lists of lists, where the inner lists contains only numbers (I want the sum of each list). Like this



1, 2, 3, 4


But not like this



1, 1, 3,3


Why this is not working?



Clear[foo];
foo[x : v___List] := Plus @@@ x /; VectorQ[v, NumberQ]


Thank you!









share|improve this question











share|improve this question




share|improve this question










asked Aug 21 at 19:43









Fernando

1233




1233











  • your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
    – kglr
    Aug 21 at 20:00

















  • your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
    – kglr
    Aug 21 at 20:00
















your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
– kglr
Aug 21 at 20:00





your condition VectorQ[v, NumberQ] for v=Sequence[1, 2, 3, 4] becomes VectorQ[1, 2, 3, 4, NumberQ] thus you get the error message VectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
– kglr
Aug 21 at 20:00











2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










ClearAll[foo];
foo[x : ___List?(VectorQ[#, NumberQ] &)] := Plus @@@ x

foo[1, 2, 2, 3, 4]



3, 9




foo[1, 2, 3]



foo[1, 2, 3]




Note: You need to use NumericQ in place of NumberQ if you want foo[1, 2, 2, 3, π] to return 3, 2 + 3 π.






share|improve this answer





























    up vote
    4
    down vote













    Another approach is to use Total, with a condition to forbid it from totaling entries with a single term:



    f[x_] := If[Length[x] > 1, Total[x], x]


    For instance:



    f /@ 1, 2, 2, 3, 4
    3, 9
    f /@ 1, 2, 2, 3, 4, 3
    3, 9, 3





    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%2f180390%2fpattern-matching-list-of-lists-containing-only-numbers%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
      6
      down vote



      accepted










      ClearAll[foo];
      foo[x : ___List?(VectorQ[#, NumberQ] &)] := Plus @@@ x

      foo[1, 2, 2, 3, 4]



      3, 9




      foo[1, 2, 3]



      foo[1, 2, 3]




      Note: You need to use NumericQ in place of NumberQ if you want foo[1, 2, 2, 3, π] to return 3, 2 + 3 π.






      share|improve this answer


























        up vote
        6
        down vote



        accepted










        ClearAll[foo];
        foo[x : ___List?(VectorQ[#, NumberQ] &)] := Plus @@@ x

        foo[1, 2, 2, 3, 4]



        3, 9




        foo[1, 2, 3]



        foo[1, 2, 3]




        Note: You need to use NumericQ in place of NumberQ if you want foo[1, 2, 2, 3, π] to return 3, 2 + 3 π.






        share|improve this answer
























          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          ClearAll[foo];
          foo[x : ___List?(VectorQ[#, NumberQ] &)] := Plus @@@ x

          foo[1, 2, 2, 3, 4]



          3, 9




          foo[1, 2, 3]



          foo[1, 2, 3]




          Note: You need to use NumericQ in place of NumberQ if you want foo[1, 2, 2, 3, π] to return 3, 2 + 3 π.






          share|improve this answer














          ClearAll[foo];
          foo[x : ___List?(VectorQ[#, NumberQ] &)] := Plus @@@ x

          foo[1, 2, 2, 3, 4]



          3, 9




          foo[1, 2, 3]



          foo[1, 2, 3]




          Note: You need to use NumericQ in place of NumberQ if you want foo[1, 2, 2, 3, π] to return 3, 2 + 3 π.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 21 at 20:05

























          answered Aug 21 at 19:57









          kglr

          158k8183380




          158k8183380




















              up vote
              4
              down vote













              Another approach is to use Total, with a condition to forbid it from totaling entries with a single term:



              f[x_] := If[Length[x] > 1, Total[x], x]


              For instance:



              f /@ 1, 2, 2, 3, 4
              3, 9
              f /@ 1, 2, 2, 3, 4, 3
              3, 9, 3





              share|improve this answer
























                up vote
                4
                down vote













                Another approach is to use Total, with a condition to forbid it from totaling entries with a single term:



                f[x_] := If[Length[x] > 1, Total[x], x]


                For instance:



                f /@ 1, 2, 2, 3, 4
                3, 9
                f /@ 1, 2, 2, 3, 4, 3
                3, 9, 3





                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  Another approach is to use Total, with a condition to forbid it from totaling entries with a single term:



                  f[x_] := If[Length[x] > 1, Total[x], x]


                  For instance:



                  f /@ 1, 2, 2, 3, 4
                  3, 9
                  f /@ 1, 2, 2, 3, 4, 3
                  3, 9, 3





                  share|improve this answer












                  Another approach is to use Total, with a condition to forbid it from totaling entries with a single term:



                  f[x_] := If[Length[x] > 1, Total[x], x]


                  For instance:



                  f /@ 1, 2, 2, 3, 4
                  3, 9
                  f /@ 1, 2, 2, 3, 4, 3
                  3, 9, 3






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 21 at 21:27









                  bill s

                  50.6k373142




                  50.6k373142



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180390%2fpattern-matching-list-of-lists-containing-only-numbers%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

                      Confectionery