Different behaviours of Cases
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
Why does Cases
output an element only in the first of the following lines?
Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> ÃÂ b, HoldPattern[a -> ÃÂ b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)
functions pattern-matching
 |Â
show 3 more comments
up vote
9
down vote
favorite
Why does Cases
output an element only in the first of the following lines?
Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> ÃÂ b, HoldPattern[a -> ÃÂ b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)
functions pattern-matching
3
compareFullForm@HoldPattern[a -> 1/2 b]
andFullForm@1, a -> 1/2 b
, the rest is about reordering.
â Kubaâ¦
Aug 23 at 8:38
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
2
UsePatternSequence
in place ofHoldPattern
â kglr
Aug 23 at 8:59
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
1
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12
 |Â
show 3 more comments
up vote
9
down vote
favorite
up vote
9
down vote
favorite
Why does Cases
output an element only in the first of the following lines?
Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> ÃÂ b, HoldPattern[a -> ÃÂ b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)
functions pattern-matching
Why does Cases
output an element only in the first of the following lines?
Cases[1, a -> 2 b, HoldPattern[a -> 2 b]]
(*a->2b*)
Cases[1, a -> 1/2 b, HoldPattern[a -> 1/2 b]]
(**)
Cases[1, a -> ÃÂ b, HoldPattern[a -> ÃÂ b]]
(**)
Cases[1, a -> c b, HoldPattern[a -> c b]]
(**)
functions pattern-matching
edited Aug 23 at 14:11
kglr
157k8182379
157k8182379
asked Aug 23 at 8:32
Giancarlo
450211
450211
3
compareFullForm@HoldPattern[a -> 1/2 b]
andFullForm@1, a -> 1/2 b
, the rest is about reordering.
â Kubaâ¦
Aug 23 at 8:38
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
2
UsePatternSequence
in place ofHoldPattern
â kglr
Aug 23 at 8:59
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
1
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12
 |Â
show 3 more comments
3
compareFullForm@HoldPattern[a -> 1/2 b]
andFullForm@1, a -> 1/2 b
, the rest is about reordering.
â Kubaâ¦
Aug 23 at 8:38
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
2
UsePatternSequence
in place ofHoldPattern
â kglr
Aug 23 at 8:59
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
1
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12
3
3
compare
FullForm@HoldPattern[a -> 1/2 b]
and FullForm@1, a -> 1/2 b
, the rest is about reordering.â Kubaâ¦
Aug 23 at 8:38
compare
FullForm@HoldPattern[a -> 1/2 b]
and FullForm@1, a -> 1/2 b
, the rest is about reordering.â Kubaâ¦
Aug 23 at 8:38
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
2
2
Use
PatternSequence
in place of HoldPattern
â kglr
Aug 23 at 8:59
Use
PatternSequence
in place of HoldPattern
â kglr
Aug 23 at 8:59
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
1
1
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12
 |Â
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
8
down vote
accepted
As an alternative to HoldPattern[Evaluate[...]
you can use PatternSequence
which evaluates its argument:
Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, PatternSequence[a -> ÃÂ b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Alternatively, give the pattern a name:
Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> ÃÂ b, p : (a -> ÃÂ b)],
Cases[1, a -> c b, p : (a -> c b)]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
up vote
5
down vote
Thanks to Kuba's comment now I see:HoldPattern
prevents the evaluation of its argument, so Cases
don't match the element in the list a->b/2
that is
Times[Rational[1, 2], b]
with the unevaluated pattern
Times[b, Power[2, -1]]
Evaluate the argument of HoldPattern
solves the problem:
Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
(*a -> b/2*)
Thanks Kuba!
You can definemyHoldPattern[x___] = HoldPattern[x];
and use in place ofHoldPattern
to reduce clutter. SincemyHoldPattern
does not hold its argument, unlike the bona fideHoldPattern
, the evaluation will have happened by the timex
is wrapped intoHoldPattern
for matching.
â kkm
Aug 23 at 9:19
add a comment |Â
up vote
2
down vote
Here is another way by using Verbatim
. Maybe it feels a bit less hacky.
Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, Verbatim[a -> ÃÂ b]],
Cases[1, a -> c b, Verbatim[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
As an alternative to HoldPattern[Evaluate[...]
you can use PatternSequence
which evaluates its argument:
Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, PatternSequence[a -> ÃÂ b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Alternatively, give the pattern a name:
Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> ÃÂ b, p : (a -> ÃÂ b)],
Cases[1, a -> c b, p : (a -> c b)]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
up vote
8
down vote
accepted
As an alternative to HoldPattern[Evaluate[...]
you can use PatternSequence
which evaluates its argument:
Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, PatternSequence[a -> ÃÂ b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Alternatively, give the pattern a name:
Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> ÃÂ b, p : (a -> ÃÂ b)],
Cases[1, a -> c b, p : (a -> c b)]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
As an alternative to HoldPattern[Evaluate[...]
you can use PatternSequence
which evaluates its argument:
Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, PatternSequence[a -> ÃÂ b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Alternatively, give the pattern a name:
Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> ÃÂ b, p : (a -> ÃÂ b)],
Cases[1, a -> c b, p : (a -> c b)]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
As an alternative to HoldPattern[Evaluate[...]
you can use PatternSequence
which evaluates its argument:
Cases[1, a -> 2 b, PatternSequence[a -> 2 b]],
Cases[1, a -> 1/2 b, PatternSequence[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, PatternSequence[a -> ÃÂ b]],
Cases[1, a -> c b, PatternSequence[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Alternatively, give the pattern a name:
Cases[1, a -> 2 b, p : (a -> 2 b)],
Cases[1, a -> 1/2 b, p : (a -> 1/2 b)],
Cases[1, a -> ÃÂ b, p : (a -> ÃÂ b)],
Cases[1, a -> c b, p : (a -> c b)]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
edited Aug 23 at 9:34
answered Aug 23 at 9:28
kglr
157k8182379
157k8182379
add a comment |Â
add a comment |Â
up vote
5
down vote
Thanks to Kuba's comment now I see:HoldPattern
prevents the evaluation of its argument, so Cases
don't match the element in the list a->b/2
that is
Times[Rational[1, 2], b]
with the unevaluated pattern
Times[b, Power[2, -1]]
Evaluate the argument of HoldPattern
solves the problem:
Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
(*a -> b/2*)
Thanks Kuba!
You can definemyHoldPattern[x___] = HoldPattern[x];
and use in place ofHoldPattern
to reduce clutter. SincemyHoldPattern
does not hold its argument, unlike the bona fideHoldPattern
, the evaluation will have happened by the timex
is wrapped intoHoldPattern
for matching.
â kkm
Aug 23 at 9:19
add a comment |Â
up vote
5
down vote
Thanks to Kuba's comment now I see:HoldPattern
prevents the evaluation of its argument, so Cases
don't match the element in the list a->b/2
that is
Times[Rational[1, 2], b]
with the unevaluated pattern
Times[b, Power[2, -1]]
Evaluate the argument of HoldPattern
solves the problem:
Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
(*a -> b/2*)
Thanks Kuba!
You can definemyHoldPattern[x___] = HoldPattern[x];
and use in place ofHoldPattern
to reduce clutter. SincemyHoldPattern
does not hold its argument, unlike the bona fideHoldPattern
, the evaluation will have happened by the timex
is wrapped intoHoldPattern
for matching.
â kkm
Aug 23 at 9:19
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Thanks to Kuba's comment now I see:HoldPattern
prevents the evaluation of its argument, so Cases
don't match the element in the list a->b/2
that is
Times[Rational[1, 2], b]
with the unevaluated pattern
Times[b, Power[2, -1]]
Evaluate the argument of HoldPattern
solves the problem:
Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
(*a -> b/2*)
Thanks Kuba!
Thanks to Kuba's comment now I see:HoldPattern
prevents the evaluation of its argument, so Cases
don't match the element in the list a->b/2
that is
Times[Rational[1, 2], b]
with the unevaluated pattern
Times[b, Power[2, -1]]
Evaluate the argument of HoldPattern
solves the problem:
Cases[1, a -> b/2, HoldPattern[Evaluate[a -> b/2]]]
(*a -> b/2*)
Thanks Kuba!
answered Aug 23 at 9:01
Giancarlo
450211
450211
You can definemyHoldPattern[x___] = HoldPattern[x];
and use in place ofHoldPattern
to reduce clutter. SincemyHoldPattern
does not hold its argument, unlike the bona fideHoldPattern
, the evaluation will have happened by the timex
is wrapped intoHoldPattern
for matching.
â kkm
Aug 23 at 9:19
add a comment |Â
You can definemyHoldPattern[x___] = HoldPattern[x];
and use in place ofHoldPattern
to reduce clutter. SincemyHoldPattern
does not hold its argument, unlike the bona fideHoldPattern
, the evaluation will have happened by the timex
is wrapped intoHoldPattern
for matching.
â kkm
Aug 23 at 9:19
You can define
myHoldPattern[x___] = HoldPattern[x];
and use in place of HoldPattern
to reduce clutter. Since myHoldPattern
does not hold its argument, unlike the bona fide HoldPattern
, the evaluation will have happened by the time x
is wrapped into HoldPattern
for matching.â kkm
Aug 23 at 9:19
You can define
myHoldPattern[x___] = HoldPattern[x];
and use in place of HoldPattern
to reduce clutter. Since myHoldPattern
does not hold its argument, unlike the bona fide HoldPattern
, the evaluation will have happened by the time x
is wrapped into HoldPattern
for matching.â kkm
Aug 23 at 9:19
add a comment |Â
up vote
2
down vote
Here is another way by using Verbatim
. Maybe it feels a bit less hacky.
Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, Verbatim[a -> ÃÂ b]],
Cases[1, a -> c b, Verbatim[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
up vote
2
down vote
Here is another way by using Verbatim
. Maybe it feels a bit less hacky.
Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, Verbatim[a -> ÃÂ b]],
Cases[1, a -> c b, Verbatim[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Here is another way by using Verbatim
. Maybe it feels a bit less hacky.
Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, Verbatim[a -> ÃÂ b]],
Cases[1, a -> c b, Verbatim[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
Here is another way by using Verbatim
. Maybe it feels a bit less hacky.
Cases[1, a -> 2 b, Verbatim[a -> 2 b]],
Cases[1, a -> 1/2 b, Verbatim[a -> 1/2 b]],
Cases[1, a -> ÃÂ b, Verbatim[a -> ÃÂ b]],
Cases[1, a -> c b, Verbatim[a -> c b]]
a -> 2 b, a -> b/2, a -> b ÃÂ, a -> b c
answered Aug 24 at 19:13
Henrik Schumacher
35.9k249102
35.9k249102
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%2f180489%2fdifferent-behaviours-of-cases%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
3
compare
FullForm@HoldPattern[a -> 1/2 b]
andFullForm@1, a -> 1/2 b
, the rest is about reordering.â Kubaâ¦
Aug 23 at 8:38
Huh, that was a nasty one!
â Henrik Schumacher
Aug 23 at 8:58
2
Use
PatternSequence
in place ofHoldPattern
â kglr
Aug 23 at 8:59
@kglr I think that is worth an answer and an explanation. (I'm personally interested.)
â Henrik Schumacher
Aug 23 at 9:01
1
See also this: mathematica.stackexchange.com/a/73020/5478
â Kubaâ¦
Aug 23 at 9:12