MathGroup Archive 2008

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

Search the Archive

Re: simple pattern match question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91181] Re: simple pattern match question
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 8 Aug 2008 07:16:48 -0400 (EDT)

On 8/7/08 at 4:42 AM, congruentialuminaire at yahoo.com wrote:

>I am trying to understand simple pattern rule evaulation. The
>following trivial examples baffles me:

>---------------------------------------------------
>In[1]:= f[x_Number] := x ^ 2
>In[2]:= f[3]
>Out[2]= f[3]

>In[3]:= f[3.3]
>Out[3]= f[3.3]

>In[4]:= NumberQ[3.3]
>Out[4]= True
>--------------------------------------------------
>Why is it that f[3.3] does not evaluate??

Because there is no built-in object with Head of Number. That is:

In[2]:= Head[3.3]

Out[2]= Real

So,

In[3]:= f[x_Real] := x^2
f[3.3]

Out[4]= 10.89

But then

In[5]:= f[3]

Out[5]= f(3)

Since,

In[6]:= Head[3]

Out[6]= Integer

So, you might want to use something more general such as

In[7]:= g[x_?NumericQ] := x^2
g[3.3]
g[3]

Out[8]= 10.89

Out[9]= 9


  • Prev by Date: Re: simple pattern match question
  • Next by Date: Re: simple pattern match question
  • Previous by thread: Re: simple pattern match question
  • Next by thread: Re: simple pattern match question