Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Conditional evaluations of functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96549] Re: Conditional evaluations of functions
  • From: Asim <maa48 at columbia.edu>
  • Date: Mon, 16 Feb 2009 06:56:58 -0500 (EST)
  • References: <gn5udh$gsv$1@smc.vnet.net> <gn8ja8$7ng$1@smc.vnet.net>

On Feb 15, 3:21 am, Jean-Marc Gulliet <jeanmarc.gull... at gmail.com>
wrote:
> In article <gn5udh$gs... at smc.vnet.net>, Asim <ma... at columbia.edu>
> wrote:
>
> > I am not sure why the function g does not work, but the function f
> > works as expected on Mathematica 7.0 on Windows XP.  I want to get th=
e
> > sum of a vector only if the argument supplied is a numeric vector.
>
> > In[1]:= g[x_?VectorQ[ x, NumericQ]] := Total[x]
>
> > In[2]:= g[{a, b , c}]
>
> > Out[2]= g[{a, b, c}]
>
> > In[3]:= g[{1, 2, 3}]
>
> > Out[3]= g[{1, 2, 3}]
>
> [snip]
>
> The test function that follows the question mark operator '?' is
> expected to be a pure function. A *pattern*, like the one used for the
> function f after the condition operator '/;' (which expect a pattern) is
> *not* a pure function.  Try
>
>   In[1]:=
>
>     g[(x_)?(VectorQ[#1, NumericQ] & )] := Total[x]
>     g[{a, b, c}]
>     g[{1, 2, 3}]
>
>   Out[2]= g[{a, b, c}]
>
>   Out[3]= 6
>
> Regards,
> --Jean-Marc

Thanks to all for your answers. Things were not clear from the
document that one needs a Pure Function.

Asim


  • Prev by Date: Re: Length of a held expression
  • Next by Date: Re: Typesetting bug
  • Previous by thread: Re: Conditional evaluations of functions
  • Next by thread: newbie here,, need help with parametrics