Re: alternating sums
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg317] Re: [mg307] alternating sums
- From: Jerry Keiper <keiper>
- Date: Mon, 12 Dec 1994 09:36:12 -0600
> I often have occasion to convert mylist = {a1,a2,a3,...} > into an alternating sum, a1-a2+a3-a4+... . One way to do it is to do In[9]:= {a1, a2, a3, a4, a5, a6, a7} Out[9]= {a1, a2, a3, a4, a5, a6, a7} In[10]:= Fold[(#2-#1)&, 0, Reverse[%]] Out[10]= a1 - a2 + a3 - a4 + a5 - a6 + a7 The Reverse is necessary to ensure that it works correctly with both even length and odd length lists. Jerry B. Keiper keiper at wri.com Wolfram Reearch, Inc.