Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.)


While I stated beforebefore that: "I can think of no way to do this with PatternTest" was not speaking rigorously and generally, but rather one-to-one substitution of methods. One can apply PatternTest to a larger expression, e.g. the entire left-hand-side, as I did for your prior questionyour prior question. If handling the entire LHS one must exercise care to prevent infinite recursion. Here is an example of how that may be done:

SetAttributes[sumsTo7, HoldFirst]
sumsTo7[_[s__]] := +s == 7

f[x__]?sumsTo7 := {x}

Now:

f[1, 2]
f[3, 4]
f[1, 2]

{3, 4}

It is possible to do this with an anonymous Function as well but it becomes rather baroque:

g[x__]?(Function[Null, Plus @@ Unevaluated[#] == 7, HoldAll]) := {x}

g[1, 2, 3]
g[-3, 10]
g[1, 2, 3]

{-3, 10}

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.)


While I stated before that: "I can think of no way to do this with PatternTest" was not speaking rigorously and generally, but rather one-to-one substitution of methods. One can apply PatternTest to a larger expression, e.g. the entire left-hand-side, as I did for your prior question. If handling the entire LHS one must exercise care to prevent infinite recursion. Here is an example of how that may be done:

SetAttributes[sumsTo7, HoldFirst]
sumsTo7[_[s__]] := +s == 7

f[x__]?sumsTo7 := {x}

Now:

f[1, 2]
f[3, 4]
f[1, 2]

{3, 4}

It is possible to do this with an anonymous Function as well but it becomes rather baroque:

g[x__]?(Function[Null, Plus @@ Unevaluated[#] == 7, HoldAll]) := {x}

g[1, 2, 3]
g[-3, 10]
g[1, 2, 3]

{-3, 10}

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.)


While I stated before that: "I can think of no way to do this with PatternTest" was not speaking rigorously and generally, but rather one-to-one substitution of methods. One can apply PatternTest to a larger expression, e.g. the entire left-hand-side, as I did for your prior question. If handling the entire LHS one must exercise care to prevent infinite recursion. Here is an example of how that may be done:

SetAttributes[sumsTo7, HoldFirst]
sumsTo7[_[s__]] := +s == 7

f[x__]?sumsTo7 := {x}

Now:

f[1, 2]
f[3, 4]
f[1, 2]

{3, 4}

It is possible to do this with an anonymous Function as well but it becomes rather baroque:

g[x__]?(Function[Null, Plus @@ Unevaluated[#] == 7, HoldAll]) := {x}

g[1, 2, 3]
g[-3, 10]
g[1, 2, 3]

{-3, 10}
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.)


While I stated before that: "I can think of no way to do this with PatternTest" was not speaking rigorously and generally, but rather one-to-one substitution of methods. One can apply PatternTest to a larger expression, e.g. the entire left-hand-side, as I did for your prior question. If handling the entire LHS one must exercise care to prevent infinite recursion. Here is an example of how that may be done:

SetAttributes[sumsTo7, HoldFirst]
sumsTo7[_[s__]] := +s == 7

f[x__]?sumsTo7 := {x}

Now:

f[1, 2]
f[3, 4]
f[1, 2]

{3, 4}

It is possible to do this with an anonymous Function as well but it becomes rather baroque:

g[x__]?(Function[Null, Plus @@ Unevaluated[#] == 7, HoldAll]) := {x}

g[1, 2, 3]
g[-3, 10]
g[1, 2, 3]

{-3, 10}