MathGroup Archive 2008

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

Search the Archive

Re: Parameter conditions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88673] Re: Parameter conditions
  • From: whatisgood <sh_liuhuashan at 163.com>
  • Date: Mon, 12 May 2008 04:46:36 -0400 (EDT)
  • References: <g07gt8$8ro$1@smc.vnet.net>

On 5=D4=C212=C8=D5, =C9=CF=CE=E73=CA=B120=B7=D6, Steven Siew <sie... at bp.com> 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

hi Steven

  try this

(*----------------------------------*)
     happy[p_?PrimeQ] := 100;
     happy[p_?(Not[PrimeQ[#]] &)] := 200;

    happy[4]
    happy[5]
(*-------------------------------------*)

Not[PrimeQ[#]] & is patten test expression, wrriten as a pure function
in Mathematica, bracket () is necessary here for consideration of
precedence of expressions.


  • Prev by Date: Re: Parameter conditions
  • Next by Date: Re: How to avoid overflow or underflow in mathematica?
  • Previous by thread: Re: Parameter conditions
  • Next by thread: Re: Parameter conditions