MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: An argument pattern problem: ranges and lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53069] Re: An argument pattern problem: ranges and lists
  • From: "Maxim A. Dubinnyi" <maxim at nmr.ru>
  • Date: Wed, 22 Dec 2004 04:53:10 -0500 (EST)
  • References: <200412180859.DAA02232@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

HoldPattern[pauli[_, _Integer?(0 <= # <= 4 &)]]

Simon Anders wrote:

>Hi,
>
>I am trying to use argument patterns to ensure that my function is only 
>applied onto arguments which have the format
>    pauli [_, _]
>where the second argument should be an integer between 0 and 4.
>
>So I defined a type
>    pauliT = HoldPattern [pauli [_, s_Integer] /; s>=0 && s<=4];
>
>and declared my function as follows:
>   f [p:pauliT] := DoSomething
>
>This works fine, but another function takes a pair of arguments
>   g [p:{pauliT,pauliT}] := DoSomethingElse
>
>This is never called, because the pattern cannot be matched.
>
>I observed:
>
>   pauliT = HoldPattern [pauli [_, s_Integer] /; s>=0 && s<=4];
>   MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>   --> False
>
>The problem seems to be that I named the second argument 's', because if 
>I delete this name, it matches:
>
>   pauliT = HoldPattern [pauli [_, s_Integer]];
>   MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>   --> False
>
>   pauliT = HoldPattern [pauli [_, _Integer]];
>   MatchQ[{pauli[1, 2], pauli[1, 3]}, {pauliT, pauliT}]
>   --> True
>
>So, how do I specify the range condition correctly?
>
>Thanks in advance,
>   Simon
>
>
>
>  
>



  • Prev by Date: Re: Help on a recursive function
  • Next by Date: Re: Mathematica is not very clever
  • Previous by thread: Re: An argument pattern problem: ranges and lists
  • Next by thread: Re: An argument pattern problem: ranges and lists