MathGroup Archive 2008

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

Search the Archive

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


  • Prev by Date: Re: Request for Collective Wisdom...
  • Next by Date: Re: Flashing Graphs With Manipulate
  • Previous by thread: Re: Parameter conditions
  • Next by thread: How to avoid overflow or underflow in mathematica?