Re: simple pattern match question

• To: mathgroup at smc.vnet.net
• Subject: [mg91167] Re: simple pattern match question
• From: Albert Retey <awnl at gmx-topmail.de>
• Date: Fri, 8 Aug 2008 07:13:55 -0400 (EDT)
• References: <g7ed78\$2po\$1@smc.vnet.net>

```congruentialuminaire at yahoo.com wrote:
> Hello UG:
>
> 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 neither 3 nor 3.3 have Head Number:

In[31]:= Head[3]
Out[31]= Integer

In[32]:= Head[3.3]
Out[32]= Real

Also NumberQ and Number are different things, actually Number is only
used to define Formats for Read. What you probably want is one of the
following:

f[x_Real]:=x^2
f[x_Integer]:=x^2
f[x_?NumberQ]:=x^2

(if playing around with these make sure to ClearAll[f] before redefining
f, otherwise the definitions will "accumulate"). Note that more often
this is useful:

f[x_?NumericQ]:=x^2

you will find more information about NumberQ, NumericQ and Heads in the
documentation...

hth,

albert

```

• Prev by Date: Re: Multiplying a vector over multiple vectors
• Next by Date: Re: Multiplying a vector over multiple vectors
• Previous by thread: Re: simple pattern match question
• Next by thread: Re: simple pattern match question