MathGroup Archive 1994

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

Search the Archive

Re: alternating sums

  • To: mathgroup at christensen.Cybernetics.NET
  • Subject: [mg326] Re: [mg307] alternating sums
  • From: Roman Maeder <maeder at inf.ethz.ch>
  • Date: Mon, 12 Dec 1994 17:02:13 +0100

> Here's an elementary question that has me partially stumped.  I often 
> have occasion to convert  mylist = {a1,a2,a3,...} into an alternating sum,
> a1-a2+a3-a4+... . I find it aesthetically unsatisfactory to generate a 
> list of alternating plus and minus ones, multiply mylist with the 
> alternating list and then Apply Plus to the product.  It works of course, 
> but I have a feeling there is a much better way to do this.  
> (This is clearly not of earthshaking importance!!.  I'm just curious.)

A variant of Fold does it nicely:

In[12]:= AlternatingSum[l_List] := -(-1)^Length[l] Fold[#2-#1&, 0, l]

In[13]:= AlternatingSum[{a1, a2, a3, a4}]

Out[13]= a1 - a2 + a3 - a4


Roman Maeder






  • Prev by Date: Answers: ParametricPlot's list of t(s).
  • Next by Date: Re: alternating sums
  • Previous by thread: Re: alternating sums
  • Next by thread: Re: alternating sums