MathGroup Archive 2002

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

Search the Archive

Re: Handling a list: Could you find a more elegant solution?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38277] Re: [mg38195] Handling a list: Could you find a more elegant solution?
  • From: Rob Pratt <rpratt at email.unc.edu>
  • Date: Tue, 10 Dec 2002 04:17:28 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On Thu, 5 Dec 2002 guillerm at usal.es wrote:

> I have a list:
>
> list1 = {a, b, c, d, e};
>
> I want manipulate the list to obtain:
>
> (*Out[]:{(a + b)/2, (b + c)/2, (c + d)/2, (d + e)/2}*)
>
> It can be done for this function
>
> f[data_List] := Drop[Plus @@ NestList[RotateRight, data,
>       1], 1]/2
>
> f[list1]
>
> but I am sure that some member of the group can find a more elegant
> function. I will appreciate to know it.
>
> Thanks
>
> Guillermo Sanchez


f[data_List] := Apply[Plus, Partition[data, 2, 1], 1] / 2


Rob Pratt
Department of Operations Research
The University of North Carolina at Chapel Hill

rpratt at email.unc.edu

http://www.unc.edu/~rpratt/



  • Prev by Date: Re Levemberg
  • Next by Date: The number of solutions to n_1 + n_2 + n_3 + ... + n_k = m
  • Previous by thread: Re:Handling a list: Could you find a more elegant solution?
  • Next by thread: Re: Handling a list: Could you find a more elegant solution?