Skip to main content
added 237 characters in body
Source Link
Jules Manson
  • 2.5k
  • 11
  • 19

A Slightly More Complex Case Where Level-1 Heads Can Vary

1. My way by defining a short alias for a long pattern because I don't know any better. However this works!

However before we start how can I add the following condition: /;x!=y preferably to cpat or inside the definition zinfy1[(x:cpat,y:cpat)/;x!=y] (does this look correct?) and not in the inside or at the end of the function body?

(* cpat for common pattern *)
(* all arguments Integers >= 0 or +Infinity *)
cpat = _Integer?(# >= 0 &) | Infinity;

zinfy1[x : cpat, y : cpat] := Style[#, 20, Bold, Green] & /@ {x, y};
zinfy1[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy1[1, 2], zinfy1[3, 2 + 3], zinfy1[Infinity, 1], zinfy1[0, 0]}

(* should fail- all args Red *)
{zinfy1[Infinity, -9], zinfy1[3, 2 - 3], zinfy1[-1 Infinity, 0], 
 zinfy1[-1, 1]}

2. An Attempt to Implement `SequencePattern` on same function and test cases completely fails.

And I don't know why. Anyone here feeling a little adventurous up for the challenge? I could sure use a little help solving this.

SequencedPattern[a_] := PatternSequence[##]?a &;
zinfy2[SequencedPattern[cpat][x_, y_]] := 
  Style[#, 20, Bold, Green] & /@ {x, y};
zinfy2[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy2[1, 2], zinfy2[3, 2 + 3], zinfy2[Infinity, 1], zinfy2[0, 0]}

(* should fail- all args Red *)
{zinfy2[Infinity, -9], zinfy2[3, 2 - 3], zinfy2[-1 Infinity, 0], 
 zinfy2[-1, 1]}

A Slightly More Complex Case Where Level-1 Heads Can Vary

1. My way by defining a short alias for a long pattern because I don't know any better. However this works!

(* cpat for common pattern *)
(* all arguments Integers >= 0 or +Infinity *)
cpat = _Integer?(# >= 0 &) | Infinity;

zinfy1[x : cpat, y : cpat] := Style[#, 20, Bold, Green] & /@ {x, y};
zinfy1[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy1[1, 2], zinfy1[3, 2 + 3], zinfy1[Infinity, 1], zinfy1[0, 0]}

(* should fail- all args Red *)
{zinfy1[Infinity, -9], zinfy1[3, 2 - 3], zinfy1[-1 Infinity, 0], 
 zinfy1[-1, 1]}

2. An Attempt to Implement `SequencePattern` on same function and test cases completely fails.

And I don't know why. Anyone here feeling a little adventurous up for the challenge? I could sure use a little help solving this.

SequencedPattern[a_] := PatternSequence[##]?a &;
zinfy2[SequencedPattern[cpat][x_, y_]] := 
  Style[#, 20, Bold, Green] & /@ {x, y};
zinfy2[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy2[1, 2], zinfy2[3, 2 + 3], zinfy2[Infinity, 1], zinfy2[0, 0]}

(* should fail- all args Red *)
{zinfy2[Infinity, -9], zinfy2[3, 2 - 3], zinfy2[-1 Infinity, 0], 
 zinfy2[-1, 1]}

A Slightly More Complex Case Where Level-1 Heads Can Vary

1. My way by defining a short alias for a long pattern because I don't know any better. However this works!

However before we start how can I add the following condition: /;x!=y preferably to cpat or inside the definition zinfy1[(x:cpat,y:cpat)/;x!=y] (does this look correct?) and not in the inside or at the end of the function body?

(* cpat for common pattern *)
(* all arguments Integers >= 0 or +Infinity *)
cpat = _Integer?(# >= 0 &) | Infinity;

zinfy1[x : cpat, y : cpat] := Style[#, 20, Bold, Green] & /@ {x, y};
zinfy1[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy1[1, 2], zinfy1[3, 2 + 3], zinfy1[Infinity, 1], zinfy1[0, 0]}

(* should fail- all args Red *)
{zinfy1[Infinity, -9], zinfy1[3, 2 - 3], zinfy1[-1 Infinity, 0], 
 zinfy1[-1, 1]}

2. An Attempt to Implement `SequencePattern` on same function and test cases completely fails.

And I don't know why. Anyone here feeling a little adventurous up for the challenge? I could sure use a little help solving this.

SequencedPattern[a_] := PatternSequence[##]?a &;
zinfy2[SequencedPattern[cpat][x_, y_]] := 
  Style[#, 20, Bold, Green] & /@ {x, y};
zinfy2[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy2[1, 2], zinfy2[3, 2 + 3], zinfy2[Infinity, 1], zinfy2[0, 0]}

(* should fail- all args Red *)
{zinfy2[Infinity, -9], zinfy2[3, 2 - 3], zinfy2[-1 Infinity, 0], 
 zinfy2[-1, 1]}
Source Link
Jules Manson
  • 2.5k
  • 11
  • 19

A Slightly More Complex Case Where Level-1 Heads Can Vary

1. My way by defining a short alias for a long pattern because I don't know any better. However this works!

(* cpat for common pattern *)
(* all arguments Integers >= 0 or +Infinity *)
cpat = _Integer?(# >= 0 &) | Infinity;

zinfy1[x : cpat, y : cpat] := Style[#, 20, Bold, Green] & /@ {x, y};
zinfy1[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy1[1, 2], zinfy1[3, 2 + 3], zinfy1[Infinity, 1], zinfy1[0, 0]}

(* should fail- all args Red *)
{zinfy1[Infinity, -9], zinfy1[3, 2 - 3], zinfy1[-1 Infinity, 0], 
 zinfy1[-1, 1]}

2. An Attempt to Implement `SequencePattern` on same function and test cases completely fails.

And I don't know why. Anyone here feeling a little adventurous up for the challenge? I could sure use a little help solving this.

SequencedPattern[a_] := PatternSequence[##]?a &;
zinfy2[SequencedPattern[cpat][x_, y_]] := 
  Style[#, 20, Bold, Green] & /@ {x, y};
zinfy2[x_, y_] := Style[#, 20, Bold, Red] & /@ {x, y};

(* should pass - all args Greeen *)
{zinfy2[1, 2], zinfy2[3, 2 + 3], zinfy2[Infinity, 1], zinfy2[0, 0]}

(* should fail- all args Red *)
{zinfy2[Infinity, -9], zinfy2[3, 2 - 3], zinfy2[-1 Infinity, 0], 
 zinfy2[-1, 1]}