MathGroup Archive 2008

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

Search the Archive

Re: Parameter conditions


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