Pattern matching list of lists containing only numbers
Clash 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!
list-manipulation pattern-matching
add a comment |Â
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!
list-manipulation pattern-matching
your conditionVectorQ[v, NumberQ]
forv=Sequence[1, 2, 3, 4]
becomesVectorQ[1, 2, 3, 4, NumberQ]
thus you get the error messageVectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
– kglr
Aug 21 at 20:00
add a comment |Â
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!
list-manipulation pattern-matching
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!
list-manipulation pattern-matching
asked Aug 21 at 19:43
Fernando
1233
1233
your conditionVectorQ[v, NumberQ]
forv=Sequence[1, 2, 3, 4]
becomesVectorQ[1, 2, 3, 4, NumberQ]
thus you get the error messageVectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
– kglr
Aug 21 at 20:00
add a comment |Â
your conditionVectorQ[v, NumberQ]
forv=Sequence[1, 2, 3, 4]
becomesVectorQ[1, 2, 3, 4, NumberQ]
thus you get the error messageVectorQ::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
add a comment |Â
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 À
.
add a comment |Â
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
add a comment |Â
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 À
.
add a comment |Â
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 À
.
add a comment |Â
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 À
.
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 À
.
edited Aug 21 at 20:05
answered Aug 21 at 19:57
kglr
158k8183380
158k8183380
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Aug 21 at 21:27


bill s
50.6k373142
50.6k373142
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
your condition
VectorQ[v, NumberQ]
forv=Sequence[1, 2, 3, 4]
becomesVectorQ[1, 2, 3, 4, NumberQ]
thus you get the error messageVectorQ::argt: VectorQ called with **3** arguments; 1 or 2 arguments are expected.
– kglr
Aug 21 at 20:00