Re: List arguments to functions
- To: mathgroup at smc.vnet.net
 - Subject: [mg45572] Re: List arguments to functions
 - From: Bill Rowe <readnewsciv at earthlink.net>
 - Date: Wed, 14 Jan 2004 01:26:31 -0500 (EST)
 - Sender: owner-wri-mathgroup at wolfram.com
 
On 1/13/04 at 4:03 AM, crozell at rice.edu (Chris Rozell) wrote:
> I have basic experience with Mathematica, but this problem has stumped 
> me for the last day or so.  Any help would be appreciated.
> I have a function that needs to take two lists as arguments, perform 
> operations on them, and then sum the resulting list.  However, by 
> including the Plus function, it operates on each list item separately 
> (which is exactly the opposite of what I need.  For a simple example,
>  >myfunc[x_, y_] = Apply[Plus, x + y];
>  >myfunc[{1, 2, 3}, {4, 5, 6}]
>  >
>  >{5, 7, 9}
 
> When what I would really like is the number 21 (i.e., 5+7+9, perform the 
> list addition first, then sum the components).
You need to use DelayedSet rather than Set, i.e. myfunc should be defined as
myfunc[x_,y_]:= Apply[Plus, x + y]
With Set, you are asking Mathematica to immediately evaluate the rhs then assign that value to myfunc. Mathematica evaluates Apply[Plus, x + y] to x + y. Consequently, it is the same as if you wrote myfunc[x_,y_]=x + y.
Read the discussion of DelayedSet and Set in the Mathematica book for more detail.
--
To reply via email subtract one hundred and four