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

I was looking at this Q&Athis Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]
(* {1, 4, 16} *)

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]
(* fTest[1, 2, 4] *)

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]
(* True *)

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]
(* {1, 4, 16} *)

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]
(* fTest[1, 2, 4] *)

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]
(* True *)

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]
(* {1, 4, 16} *)

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]
(* fTest[1, 2, 4] *)

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]
(* True *)

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

deleted 125 characters in body
Source Link
khanhnguyendata
  • 5.2k
  • 1
  • 23
  • 33

I was looking at this Q&A about using pattern test (p_pattern_?test) vs pattern condition (p_pattern_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

f3[x__]Clear[fCond]
fCond[x__ /; Plus[x] == 77] := {x}^2
fCond[1, 2, 4]
(* {1, 4, 16} *)

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]
(* fTest[1, 2, 4] *)

Mathematica graphics

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]
(* True *)

Mathematica graphics

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

PS: pattern condition works as expected

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]

Mathematica graphics

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

I was looking at this Q&A about using pattern test (p_?test) vs pattern condition (p_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

f3[x__] /; Plus[x] == 7 := {x}^2

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]

Mathematica graphics

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]

Mathematica graphics

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

PS: pattern condition works as expected

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]

Mathematica graphics

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]
(* {1, 4, 16} *)

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]
(* fTest[1, 2, 4] *)

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]
(* True *)

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498

Post Reopened by Mr.Wizard
Post Closed as "Not suitable for this site" by Michael E2, m_goldberg, bobthechemist, Jens, ciao
Source Link
khanhnguyendata
  • 5.2k
  • 1
  • 23
  • 33

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (p_?test) vs pattern condition (p_/;cond) and came across this example where using condition was the only possible way to restrict the function parameters (function definition added by me):

f3[x__] /; Plus[x] == 7 := {x}^2

I know that, aside from the built-in tests in MMA (IntegerQ, NumericQ, eg), one can write custom pattern tests using pure functions [2]:

test pure function

Hence, I was trying to be clever by using tests with sequence pattern (__) coupled with sequence argument for pure function (##). However, this did not work when plugging in values for the parameter/pattern.

Clear[fTest]
fTest[x__?(Plus[##] == 7 &)] := {x}^2
fTest[1, 2, 4]

Mathematica graphics

, even though the pure function test alone does work with the plugged in sequence

Plus[##] == 7 &[1, 2, 4]

Mathematica graphics

Why doesn't pattern test work via this method? If so, is there any other way to do pattern test as opposed to pattern condition in this case?

PS: pattern condition works as expected

Clear[fCond]
fCond[x__ /; Plus[x] == 7] := {x}^2
fCond[1, 2, 4]

Mathematica graphics

[2]: Ruskeepaa's Mathematica Navigator 2009 p.498