Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: List arguments to functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45559] Re: [mg45537] List arguments to functions
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Wed, 14 Jan 2004 01:26:13 -0500 (EST)
  • References: <200401130903.EAA10620@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 13 Jan 2004, at 09:03, 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).  In this simple example 
> I
> could perform the summation after the function returns, but in my real
> problem this would not be possible.
>
> When reading the documentation, it appears that the Plus function
> behaves this way because it has the attribute "Flat".  But even after
> removing that attribute it still behaves in the same way, so I may be
> misunderstanding the description of "flat".  Can anyone suggest a way 
> to
> either modify the Plus function behavior, or do this another way?  
> Thank
> you in advance!
>
>
Your problem has nothing to do with the attributes of Plus but is 
simply caused by your using  = (Set) instead of := (SetDelayed).

Thus:


myfunc[x_,y_]:=Apply[Plus,x+y];


myfunc[{1,2,3},{4,5,6}]

21


Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Struggling with list element assignment in functions
  • Next by Date: Re: what is the general theory of extracting solutions from DSolve (and similar) functions
  • Previous by thread: Re: List arguments to functions
  • Next by thread: Re: List arguments to functions