Re: Conditional evaluations of functions
- To: mathgroup at smc.vnet.net
- Subject: [mg96514] Re: Conditional evaluations of functions
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 15 Feb 2009 03:21:34 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <gn5udh$gsv$1@smc.vnet.net>
In article <gn5udh$gsv$1 at smc.vnet.net>, Asim <maa48 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 the > 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