About shorthand notation for conditional pattern (Paul Wellin âEssentials of Programming in Mathematicaâ)

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I am reading Paul Wellin "Essentials of Programming in Mathematica".
Mr. Wellin wrote as follows in his above book:
"There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."
MatchQ[Range[10], s__/;NumberQ[s]]
returns error and False. And,
MatchQ[Range[10], s__?NumberQ]
returns True.
Why?
pattern-matching expression-test
add a comment |Â
up vote
3
down vote
favorite
I am reading Paul Wellin "Essentials of Programming in Mathematica".
Mr. Wellin wrote as follows in his above book:
"There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."
MatchQ[Range[10], s__/;NumberQ[s]]
returns error and False. And,
MatchQ[Range[10], s__?NumberQ]
returns True.
Why?
pattern-matching expression-test
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am reading Paul Wellin "Essentials of Programming in Mathematica".
Mr. Wellin wrote as follows in his above book:
"There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."
MatchQ[Range[10], s__/;NumberQ[s]]
returns error and False. And,
MatchQ[Range[10], s__?NumberQ]
returns True.
Why?
pattern-matching expression-test
I am reading Paul Wellin "Essentials of Programming in Mathematica".
Mr. Wellin wrote as follows in his above book:
"There is a convenient shorhand notation for conditional patterns that is commonly used. The condition expr_/;test can be shortened to expr_?test."
MatchQ[Range[10], s__/;NumberQ[s]]
returns error and False. And,
MatchQ[Range[10], s__?NumberQ]
returns True.
Why?
pattern-matching expression-test
pattern-matching expression-test
edited 7 mins ago
J. M. is somewhat okay.â¦
94.5k10293454
94.5k10293454
asked 54 mins ago
tchappy ha
1283
1283
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
__?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.
The third bullet point under "Details" in the documentation for PatterTest (?) is this:
In a form such as __?test, every element in the sequence matched by __
must yield True when test is applied.
MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate
NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:
MatchQ[Range[10], s__ /; NumberQ[s]]
but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.
To actually get what we want using this method, we have to do this:
MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]
__?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.
add a comment |Â
up vote
0
down vote
In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)
What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.
In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.
In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.
If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:
MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
__?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.
The third bullet point under "Details" in the documentation for PatterTest (?) is this:
In a form such as __?test, every element in the sequence matched by __
must yield True when test is applied.
MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate
NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:
MatchQ[Range[10], s__ /; NumberQ[s]]
but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.
To actually get what we want using this method, we have to do this:
MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]
__?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.
add a comment |Â
up vote
2
down vote
__?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.
The third bullet point under "Details" in the documentation for PatterTest (?) is this:
In a form such as __?test, every element in the sequence matched by __
must yield True when test is applied.
MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate
NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:
MatchQ[Range[10], s__ /; NumberQ[s]]
but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.
To actually get what we want using this method, we have to do this:
MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]
__?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
__?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.
The third bullet point under "Details" in the documentation for PatterTest (?) is this:
In a form such as __?test, every element in the sequence matched by __
must yield True when test is applied.
MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate
NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:
MatchQ[Range[10], s__ /; NumberQ[s]]
but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.
To actually get what we want using this method, we have to do this:
MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]
__?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.
__?test is not a shorthand for s__ /; test[s] in the sense that it is an alias. There are major differences that one needs to be aware of.
The third bullet point under "Details" in the documentation for PatterTest (?) is this:
In a form such as __?test, every element in the sequence matched by __
must yield True when test is applied.
MatchQ[Range[10], s__ /; NumberQ[s]] will evaluate
NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This will give an error (and return False) because NumberQ hasn't been defined to take that many arguments. You can fix it, in a sense, like this:
MatchQ[Range[10], s__ /; NumberQ[s]]
but now it returns False because NumberQ[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sees a list, which is not a number.
To actually get what we want using this method, we have to do this:
MatchQ[Range[10], s__ /; AllTrue[s, NumberQ]]
__?test is much shorter for this type of test, but as we have seen it is not a shorthand in the sense that we can use it as a drop-in replacement wherever we want. ? essentially does the AllTrue[s, test] part for us.
answered 19 mins ago
C. E.
48.1k392194
48.1k392194
add a comment |Â
add a comment |Â
up vote
0
down vote
In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)
What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.
In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.
In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.
If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:
MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
add a comment |Â
up vote
0
down vote
In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)
What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.
In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.
In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.
If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:
MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)
What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.
In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.
In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.
If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:
MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]
In short: you are confusing Blank (_) and BlankSequence (__). (There is also BlankNullSequence (___), but I shall skip talking about that to keep it short.)
What Wellin said about the patterns involving Blank (_) is mostly true, in that one has the choice of using Condition (/;) or PatternTest (?). So, you can do n_?NumberQ (the form often preferred for relatively simple expression tests) or n_ /; NumberQ[n] (more often used if the expression test is sufficiently complicated, e.g. n_ /; NumberQ[n] && Positive[n]). It is possible to express one in terms of the other, but the preferences I gave are common.
In your attempt, you were trying to use /; with BlankSequence (__); recall that __ is supposed to match one or more things, when the expression test NumberQ can only take one argument. So NumericQ[s] will choke if s was e.g. 1, 2, as you are now trying to give it two arguments.
In contrast, the pattern s__?NumberQ is fine; it is the way to test that s is one or more things that satisfy the expression test NumericQ.
If you had really insisted on using /; for this, one way in this case is to use something like VectorQ:
MatchQ[Range[10], s__ /; VectorQ[s, NumberQ]]
answered 9 mins ago
J. M. is somewhat okay.â¦
94.5k10293454
94.5k10293454
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
add a comment |Â
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins ago
(I am writing from a smartphone, so please edit my answer if the formatting looks screwy on desktop.)
â J. M. is somewhat okay.â¦
8 mins 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%2f183750%2fabout-shorthand-notation-for-conditional-pattern-paul-wellin-essentials-of-pro%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
