Why does MatchQ[a, r_ /; Head[r] != Plus] evaluate to False?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I'm trying to understand. Why is it that:
MatchQ[a, r_ /; Head[r] != Plus]
Evaluates to:
False
? For me, I would think that, because:
Head[a]
Evaluates to:
Symbol
Where a
has no value or expression assigned to it, then this:
MatchQ[a, r_ /; Head[r] != Plus]
Should evaluate to True
. Could someone point me in the right direction to understanding this better? Thanks!
pattern-matching head
add a comment |Â
up vote
3
down vote
favorite
I'm trying to understand. Why is it that:
MatchQ[a, r_ /; Head[r] != Plus]
Evaluates to:
False
? For me, I would think that, because:
Head[a]
Evaluates to:
Symbol
Where a
has no value or expression assigned to it, then this:
MatchQ[a, r_ /; Head[r] != Plus]
Should evaluate to True
. Could someone point me in the right direction to understanding this better? Thanks!
pattern-matching head
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm trying to understand. Why is it that:
MatchQ[a, r_ /; Head[r] != Plus]
Evaluates to:
False
? For me, I would think that, because:
Head[a]
Evaluates to:
Symbol
Where a
has no value or expression assigned to it, then this:
MatchQ[a, r_ /; Head[r] != Plus]
Should evaluate to True
. Could someone point me in the right direction to understanding this better? Thanks!
pattern-matching head
I'm trying to understand. Why is it that:
MatchQ[a, r_ /; Head[r] != Plus]
Evaluates to:
False
? For me, I would think that, because:
Head[a]
Evaluates to:
Symbol
Where a
has no value or expression assigned to it, then this:
MatchQ[a, r_ /; Head[r] != Plus]
Should evaluate to True
. Could someone point me in the right direction to understanding this better? Thanks!
pattern-matching head
pattern-matching head
asked 1 hour ago
Jmeeks29ig
39518
39518
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
The problem is this:
If a
is a symbol then its Head
is Symbol
, so Head[a] =!= Plus
reduces to Symbol != Plus
. Unequal
(!=
) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus
should evaluate to True
or to False
, since both sides are Symbol
s. Here an example why this is undecidable with the current amount of information:
With[Symbol = 1, Plus = 1, Symbol != Plus]
With[Symbol = 1, Plus = 0, Symbol != Plus]
False
True
So the expression Symbol != Plus
stays unevaluated. (This is the best strategy since later definitions could make it decidable.)
Because the second argument of Condition
(/;
) does not evaluate to True
, the pattern does not match.
Lesson to learn: For testing for structual inequality, use UnsameQ
(=!=
):
MatchQ[a, r_ /; Head[r] =!= Plus]
True
Of course, the same applies, mutatis mutandis, to Equal
and SameQ
.
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
The problem is this:
If a
is a symbol then its Head
is Symbol
, so Head[a] =!= Plus
reduces to Symbol != Plus
. Unequal
(!=
) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus
should evaluate to True
or to False
, since both sides are Symbol
s. Here an example why this is undecidable with the current amount of information:
With[Symbol = 1, Plus = 1, Symbol != Plus]
With[Symbol = 1, Plus = 0, Symbol != Plus]
False
True
So the expression Symbol != Plus
stays unevaluated. (This is the best strategy since later definitions could make it decidable.)
Because the second argument of Condition
(/;
) does not evaluate to True
, the pattern does not match.
Lesson to learn: For testing for structual inequality, use UnsameQ
(=!=
):
MatchQ[a, r_ /; Head[r] =!= Plus]
True
Of course, the same applies, mutatis mutandis, to Equal
and SameQ
.
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
add a comment |Â
up vote
5
down vote
accepted
The problem is this:
If a
is a symbol then its Head
is Symbol
, so Head[a] =!= Plus
reduces to Symbol != Plus
. Unequal
(!=
) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus
should evaluate to True
or to False
, since both sides are Symbol
s. Here an example why this is undecidable with the current amount of information:
With[Symbol = 1, Plus = 1, Symbol != Plus]
With[Symbol = 1, Plus = 0, Symbol != Plus]
False
True
So the expression Symbol != Plus
stays unevaluated. (This is the best strategy since later definitions could make it decidable.)
Because the second argument of Condition
(/;
) does not evaluate to True
, the pattern does not match.
Lesson to learn: For testing for structual inequality, use UnsameQ
(=!=
):
MatchQ[a, r_ /; Head[r] =!= Plus]
True
Of course, the same applies, mutatis mutandis, to Equal
and SameQ
.
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
The problem is this:
If a
is a symbol then its Head
is Symbol
, so Head[a] =!= Plus
reduces to Symbol != Plus
. Unequal
(!=
) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus
should evaluate to True
or to False
, since both sides are Symbol
s. Here an example why this is undecidable with the current amount of information:
With[Symbol = 1, Plus = 1, Symbol != Plus]
With[Symbol = 1, Plus = 0, Symbol != Plus]
False
True
So the expression Symbol != Plus
stays unevaluated. (This is the best strategy since later definitions could make it decidable.)
Because the second argument of Condition
(/;
) does not evaluate to True
, the pattern does not match.
Lesson to learn: For testing for structual inequality, use UnsameQ
(=!=
):
MatchQ[a, r_ /; Head[r] =!= Plus]
True
Of course, the same applies, mutatis mutandis, to Equal
and SameQ
.
The problem is this:
If a
is a symbol then its Head
is Symbol
, so Head[a] =!= Plus
reduces to Symbol != Plus
. Unequal
(!=
) is supposed to be a mathematical test for inequality. In this case, it just cannot decide whether Symbol != Plus
should evaluate to True
or to False
, since both sides are Symbol
s. Here an example why this is undecidable with the current amount of information:
With[Symbol = 1, Plus = 1, Symbol != Plus]
With[Symbol = 1, Plus = 0, Symbol != Plus]
False
True
So the expression Symbol != Plus
stays unevaluated. (This is the best strategy since later definitions could make it decidable.)
Because the second argument of Condition
(/;
) does not evaluate to True
, the pattern does not match.
Lesson to learn: For testing for structual inequality, use UnsameQ
(=!=
):
MatchQ[a, r_ /; Head[r] =!= Plus]
True
Of course, the same applies, mutatis mutandis, to Equal
and SameQ
.
edited 1 hour ago
answered 1 hour ago
Henrik Schumacher
38.4k251112
38.4k251112
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
add a comment |Â
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
Awesome, thanks! That helps a lot!
â Jmeeks29ig
1 hour ago
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%2f182059%2fwhy-does-matchqa-r-headr-plus-evaluate-to-false%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