Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg53007] Re: [mg52973] An argument pattern problem: ranges and lists
  • From: DrBob <drbob at bigfoot.com>
  • Date: Sun, 19 Dec 2004 06:15:06 -0500 (EST)
  • References: <200412180859.DAA02232@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

Try this:

pauliT = pauli[_, _Integer?(0 â?¤ # â?¤ 4 &)];

Clear[f,g]
f[p:pauliT]:=DoSomething
f@pauli[a,3]

DoSomething

g[p:{pauliT,pauliT}]:=DoSomethingElse
g[{pauli[a, 3], pauli[a, 4]}]

DoSomethingElse

If you need to refer to what you were calling s, do it through p using Part, or something like this:

Clear[g]
g[p:{one:pauliT, two:pauliT}] := DoSomethingElse[s1 == one[[-1]], s2 == two[[-1]]]

g[{pauli[a, 3], pauli[a, 4]}]
DoSomethingElse[s1 == 3, s2 == 4]

Bobby

On Sat, 18 Dec 2004 03:59:53 -0500 (EST), Simon Anders <simon.anders at uibk.ac.at> 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
>
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: An argument pattern problem: ranges and lists
  • Next by Date: Re: Mathematica language issues
  • Previous by thread: An argument pattern problem: ranges and lists
  • Next by thread: Re: An argument pattern problem: ranges and lists