       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:= g[x_?VectorQ[ x, NumericQ]] := Total[x]
>
> In:= g[{a, b , c}]
>
> Out= g[{a, b, c}]
>
> In:= g[{1, 2, 3}]
>
> Out= 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:=

g[(x_)?(VectorQ[#1, NumericQ] & )] := Total[x]
g[{a, b, c}]
g[{1, 2, 3}]

Out= g[{a, b, c}]

Out= 6

Regards,
--Jean-Marc

```

• Prev by Date: Problems about "For"
• Next by Date: Re: Benchmark performance Under AIX
• Previous by thread: Re: Conditional evaluations of functions
• Next by thread: Re: Conditional evaluations of functions