Re: Parameter conditions
- To: mathgroup at smc.vnet.net
- Subject: [mg88711] Re: Parameter conditions
- From: whatisgood <sh_liuhuashan at 163.com>
- Date: Tue, 13 May 2008 07:11:17 -0400 (EDT)
- References: <g07gt8$8ro$1@smc.vnet.net> <g0907b$ksv$1@smc.vnet.net>
On 5=D4=C212=C8=D5, =CF=C2=CE=E74=CA=B147=B7=D6, Szabolcs Horv=A8=A2t <szhor= ... at gmail.com> wrote: > Steven Siew wrote: > > I'm just curious why the simple code below is not working. > > > Why is happy[4] not returning the number 200 > > > In[1]:= happy[p_?PrimeQ] = 100; > > happy[p_?Not[PrimeQ]] = 200; > > > In[3]:= Inhappy[4] > > Out[3]:= happy[4] > > > In[4]:= happy[5] > > Out[4]:= 100 > > The correct code is > > happy[p_? PrimeQ] = 100; > happy[p_? (Not[PrimeQ[#]]&) ] = 200; > > PatternTest (the ? operator) should be followed by a function. > Not[PrimeQ] is not a function, but we can build a suitable function > easily: Not[PrimeQ[#]]&. Now we just have to pay attention to use > proper bracketing around it (because happy[p_? Not[PrimeQ[#]]& ] is > equivalent to happy[(p_?Not[PrimeQ[#]]&)], which does not make sense). > > But in this simple case > > happy[p_? PrimeQ] = 100; > happy[p_] = 200; > > will do the same thing. elegant