Making a list of patterns of arbitrary length
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
I would like to create a list of arbitrary length:
x1_, x2_, ...
Where each of the elements of the list has the full form:
Pattern[xi, Blank]
This answer shows how to create a list of symbols:
x1, x2, ...
but I don't know how to adapt that to obtain the above.
I intend to use this list in the definition of a function as in here.
list-manipulation functions pattern-matching
add a comment |Â
up vote
10
down vote
favorite
I would like to create a list of arbitrary length:
x1_, x2_, ...
Where each of the elements of the list has the full form:
Pattern[xi, Blank]
This answer shows how to create a list of symbols:
x1, x2, ...
but I don't know how to adapt that to obtain the above.
I intend to use this list in the definition of a function as in here.
list-manipulation functions pattern-matching
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I would like to create a list of arbitrary length:
x1_, x2_, ...
Where each of the elements of the list has the full form:
Pattern[xi, Blank]
This answer shows how to create a list of symbols:
x1, x2, ...
but I don't know how to adapt that to obtain the above.
I intend to use this list in the definition of a function as in here.
list-manipulation functions pattern-matching
I would like to create a list of arbitrary length:
x1_, x2_, ...
Where each of the elements of the list has the full form:
Pattern[xi, Blank]
This answer shows how to create a list of symbols:
x1, x2, ...
but I don't know how to adapt that to obtain the above.
I intend to use this list in the definition of a function as in here.
list-manipulation functions pattern-matching
edited Aug 28 at 2:18


m_goldberg
81.7k869187
81.7k869187
asked Aug 28 at 0:45


Winkelried
1634
1634
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
8
down vote
accepted
Array[ToExpression["x" <> ToString @ # <> "_"] &, 5]
x1_, x2_, x3_, x4_, x5_
FullForm @ %
List[Pattern[x1,Blank], Pattern[x2, Blank], Pattern[x3, Blank], Pattern[x4, Blank], Pattern[x5, Blank]]
Also
Thread[Pattern[Evaluate@Array[Symbol["x" <> ToString@#] &, 5], Blank]]
x1_, x2_, x3_, x4_, x5_
and
ToExpression[Table["x" <> i <> "_", i, ToString /@ Range[5]]]
x1_, x2_, x3_, x4_, x5_
1
The middle one will fail ifx1=1
.
– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
add a comment |Â
up vote
8
down vote
If you construct a list of strings instead, you can take advantage of the fact that ToExpression
is listable, and supports a 3rd argument that post-processes the output. For example:
ToExpression[
"x1", "x2", "x3",
StandardForm,
Pattern[#,Blank]&
]
x1_, x2_, x3_
Or, creating the list and converting:
ToExpression[
Table["x" <> ToString@i, i, 5],
StandardForm,
Pattern[#, Blank]&
]
x1_, x2_, x3_, x4_, x5_
2
It won't work ifx1 = 1
.
– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead ofPattern[#,Blank]&
as usual.
– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean byx1 = 1
?
– Winkelried
Aug 28 at 8:57
@Winkelried if any ofxi
have values prior to that evaluation then you will get e.g.Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
add a comment |Â
up vote
4
down vote
Nothing new but shorter:
StringTemplate["x``_"] /@ Range[10] // ToExpression
x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_
add a comment |Â
up vote
3
down vote
Here is an example of how to make the solution in the link work for this case:
patt = Table[
With[
s = Symbol["x" <> ToString[i]],
Pattern[s, Blank]
], i, 10];
Range[10] /. patt :> x5, x8
5, 8
Using With
here is a trick to insert the symbol into Pattern
. Since Pattern
has the attribute HoldFirst
, it would not work to write e.g.
Pattern[Symbol["x" <> ToString[i]], Blank]
because Symbol["x" <> ToString[i]]
would not be evaluated before it was passed to Pattern
, i.e. Pattern
would not receive a string as is required.
It won't work ifx1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
Array[ToExpression["x" <> ToString @ # <> "_"] &, 5]
x1_, x2_, x3_, x4_, x5_
FullForm @ %
List[Pattern[x1,Blank], Pattern[x2, Blank], Pattern[x3, Blank], Pattern[x4, Blank], Pattern[x5, Blank]]
Also
Thread[Pattern[Evaluate@Array[Symbol["x" <> ToString@#] &, 5], Blank]]
x1_, x2_, x3_, x4_, x5_
and
ToExpression[Table["x" <> i <> "_", i, ToString /@ Range[5]]]
x1_, x2_, x3_, x4_, x5_
1
The middle one will fail ifx1=1
.
– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
add a comment |Â
up vote
8
down vote
accepted
Array[ToExpression["x" <> ToString @ # <> "_"] &, 5]
x1_, x2_, x3_, x4_, x5_
FullForm @ %
List[Pattern[x1,Blank], Pattern[x2, Blank], Pattern[x3, Blank], Pattern[x4, Blank], Pattern[x5, Blank]]
Also
Thread[Pattern[Evaluate@Array[Symbol["x" <> ToString@#] &, 5], Blank]]
x1_, x2_, x3_, x4_, x5_
and
ToExpression[Table["x" <> i <> "_", i, ToString /@ Range[5]]]
x1_, x2_, x3_, x4_, x5_
1
The middle one will fail ifx1=1
.
– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Array[ToExpression["x" <> ToString @ # <> "_"] &, 5]
x1_, x2_, x3_, x4_, x5_
FullForm @ %
List[Pattern[x1,Blank], Pattern[x2, Blank], Pattern[x3, Blank], Pattern[x4, Blank], Pattern[x5, Blank]]
Also
Thread[Pattern[Evaluate@Array[Symbol["x" <> ToString@#] &, 5], Blank]]
x1_, x2_, x3_, x4_, x5_
and
ToExpression[Table["x" <> i <> "_", i, ToString /@ Range[5]]]
x1_, x2_, x3_, x4_, x5_
Array[ToExpression["x" <> ToString @ # <> "_"] &, 5]
x1_, x2_, x3_, x4_, x5_
FullForm @ %
List[Pattern[x1,Blank], Pattern[x2, Blank], Pattern[x3, Blank], Pattern[x4, Blank], Pattern[x5, Blank]]
Also
Thread[Pattern[Evaluate@Array[Symbol["x" <> ToString@#] &, 5], Blank]]
x1_, x2_, x3_, x4_, x5_
and
ToExpression[Table["x" <> i <> "_", i, ToString /@ Range[5]]]
x1_, x2_, x3_, x4_, x5_
edited Aug 28 at 4:10
answered Aug 28 at 1:54
kglr
158k8183382
158k8183382
1
The middle one will fail ifx1=1
.
– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
add a comment |Â
1
The middle one will fail ifx1=1
.
– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
1
1
The middle one will fail if
x1=1
.– Kuba♦
Aug 28 at 7:37
The middle one will fail if
x1=1
.– Kuba♦
Aug 28 at 7:37
@Kuba, good point.
– kglr
Aug 28 at 8:09
@Kuba, good point.
– kglr
Aug 28 at 8:09
add a comment |Â
up vote
8
down vote
If you construct a list of strings instead, you can take advantage of the fact that ToExpression
is listable, and supports a 3rd argument that post-processes the output. For example:
ToExpression[
"x1", "x2", "x3",
StandardForm,
Pattern[#,Blank]&
]
x1_, x2_, x3_
Or, creating the list and converting:
ToExpression[
Table["x" <> ToString@i, i, 5],
StandardForm,
Pattern[#, Blank]&
]
x1_, x2_, x3_, x4_, x5_
2
It won't work ifx1 = 1
.
– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead ofPattern[#,Blank]&
as usual.
– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean byx1 = 1
?
– Winkelried
Aug 28 at 8:57
@Winkelried if any ofxi
have values prior to that evaluation then you will get e.g.Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
add a comment |Â
up vote
8
down vote
If you construct a list of strings instead, you can take advantage of the fact that ToExpression
is listable, and supports a 3rd argument that post-processes the output. For example:
ToExpression[
"x1", "x2", "x3",
StandardForm,
Pattern[#,Blank]&
]
x1_, x2_, x3_
Or, creating the list and converting:
ToExpression[
Table["x" <> ToString@i, i, 5],
StandardForm,
Pattern[#, Blank]&
]
x1_, x2_, x3_, x4_, x5_
2
It won't work ifx1 = 1
.
– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead ofPattern[#,Blank]&
as usual.
– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean byx1 = 1
?
– Winkelried
Aug 28 at 8:57
@Winkelried if any ofxi
have values prior to that evaluation then you will get e.g.Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
add a comment |Â
up vote
8
down vote
up vote
8
down vote
If you construct a list of strings instead, you can take advantage of the fact that ToExpression
is listable, and supports a 3rd argument that post-processes the output. For example:
ToExpression[
"x1", "x2", "x3",
StandardForm,
Pattern[#,Blank]&
]
x1_, x2_, x3_
Or, creating the list and converting:
ToExpression[
Table["x" <> ToString@i, i, 5],
StandardForm,
Pattern[#, Blank]&
]
x1_, x2_, x3_, x4_, x5_
If you construct a list of strings instead, you can take advantage of the fact that ToExpression
is listable, and supports a 3rd argument that post-processes the output. For example:
ToExpression[
"x1", "x2", "x3",
StandardForm,
Pattern[#,Blank]&
]
x1_, x2_, x3_
Or, creating the list and converting:
ToExpression[
Table["x" <> ToString@i, i, 5],
StandardForm,
Pattern[#, Blank]&
]
x1_, x2_, x3_, x4_, x5_
answered Aug 28 at 1:31


Carl Woll
56k272146
56k272146
2
It won't work ifx1 = 1
.
– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead ofPattern[#,Blank]&
as usual.
– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean byx1 = 1
?
– Winkelried
Aug 28 at 8:57
@Winkelried if any ofxi
have values prior to that evaluation then you will get e.g.Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
add a comment |Â
2
It won't work ifx1 = 1
.
– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead ofPattern[#,Blank]&
as usual.
– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean byx1 = 1
?
– Winkelried
Aug 28 at 8:57
@Winkelried if any ofxi
have values prior to that evaluation then you will get e.g.Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
2
2
It won't work if
x1 = 1
.– Kuba♦
Aug 28 at 7:36
It won't work if
x1 = 1
.– Kuba♦
Aug 28 at 7:36
Function[sym,Pattern[sym,Blank],HoldFirst]
instead of Pattern[#,Blank]&
as usual.– Anton.Sakovich
Aug 28 at 7:44
Function[sym,Pattern[sym,Blank],HoldFirst]
instead of Pattern[#,Blank]&
as usual.– Anton.Sakovich
Aug 28 at 7:44
@Kuba what exactly do you mean by
x1 = 1
?– Winkelried
Aug 28 at 8:57
@Kuba what exactly do you mean by
x1 = 1
?– Winkelried
Aug 28 at 8:57
@Winkelried if any of
xi
have values prior to that evaluation then you will get e.g. Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
@Winkelried if any of
xi
have values prior to that evaluation then you will get e.g. Pattern[1, Blank]
– Kuba♦
Aug 28 at 8:58
add a comment |Â
up vote
4
down vote
Nothing new but shorter:
StringTemplate["x``_"] /@ Range[10] // ToExpression
x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_
add a comment |Â
up vote
4
down vote
Nothing new but shorter:
StringTemplate["x``_"] /@ Range[10] // ToExpression
x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Nothing new but shorter:
StringTemplate["x``_"] /@ Range[10] // ToExpression
x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_
Nothing new but shorter:
StringTemplate["x``_"] /@ Range[10] // ToExpression
x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_
answered Aug 28 at 7:38


Kuba♦
99.3k11194491
99.3k11194491
add a comment |Â
add a comment |Â
up vote
3
down vote
Here is an example of how to make the solution in the link work for this case:
patt = Table[
With[
s = Symbol["x" <> ToString[i]],
Pattern[s, Blank]
], i, 10];
Range[10] /. patt :> x5, x8
5, 8
Using With
here is a trick to insert the symbol into Pattern
. Since Pattern
has the attribute HoldFirst
, it would not work to write e.g.
Pattern[Symbol["x" <> ToString[i]], Blank]
because Symbol["x" <> ToString[i]]
would not be evaluated before it was passed to Pattern
, i.e. Pattern
would not receive a string as is required.
It won't work ifx1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
add a comment |Â
up vote
3
down vote
Here is an example of how to make the solution in the link work for this case:
patt = Table[
With[
s = Symbol["x" <> ToString[i]],
Pattern[s, Blank]
], i, 10];
Range[10] /. patt :> x5, x8
5, 8
Using With
here is a trick to insert the symbol into Pattern
. Since Pattern
has the attribute HoldFirst
, it would not work to write e.g.
Pattern[Symbol["x" <> ToString[i]], Blank]
because Symbol["x" <> ToString[i]]
would not be evaluated before it was passed to Pattern
, i.e. Pattern
would not receive a string as is required.
It won't work ifx1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Here is an example of how to make the solution in the link work for this case:
patt = Table[
With[
s = Symbol["x" <> ToString[i]],
Pattern[s, Blank]
], i, 10];
Range[10] /. patt :> x5, x8
5, 8
Using With
here is a trick to insert the symbol into Pattern
. Since Pattern
has the attribute HoldFirst
, it would not work to write e.g.
Pattern[Symbol["x" <> ToString[i]], Blank]
because Symbol["x" <> ToString[i]]
would not be evaluated before it was passed to Pattern
, i.e. Pattern
would not receive a string as is required.
Here is an example of how to make the solution in the link work for this case:
patt = Table[
With[
s = Symbol["x" <> ToString[i]],
Pattern[s, Blank]
], i, 10];
Range[10] /. patt :> x5, x8
5, 8
Using With
here is a trick to insert the symbol into Pattern
. Since Pattern
has the attribute HoldFirst
, it would not work to write e.g.
Pattern[Symbol["x" <> ToString[i]], Blank]
because Symbol["x" <> ToString[i]]
would not be evaluated before it was passed to Pattern
, i.e. Pattern
would not receive a string as is required.
edited Aug 28 at 9:49
answered Aug 28 at 1:19
C. E.
47.6k390192
47.6k390192
It won't work ifx1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
add a comment |Â
It won't work ifx1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
It won't work if
x1 = 1
– Kuba♦
Aug 28 at 7:36
It won't work if
x1 = 1
– Kuba♦
Aug 28 at 7:36
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
@Kuba You mean if the symbols have values?
– C. E.
Aug 28 at 9:48
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
Yes, sorry for not being clear.
– Kuba♦
Aug 28 at 9:54
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
@Kuba Well, you are right. I wanted to show how to adapt the answer OP linked to, but it has this flaw.
– C. E.
Aug 28 at 10:40
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%2f180763%2fmaking-a-list-of-patterns-of-arbitrary-length%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