Different behaviours of Cases
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
functions pattern-matching
edited Aug 23 at 14:11
kglr
173k8194400
173k8194400
asked Aug 23 at 8:32
Giancarlo
452311
452311
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
173k8194400
173k8194400
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
452311
452311
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
45.6k466132
45.6k466132
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
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