Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
- To: mathgroup at smc.vnet.net
- Subject: [mg24920] Re: [mg24892] functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
- From: Rob Pratt <rpratt at email.unc.edu>
- Date: Sun, 20 Aug 2000 01:35:11 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Not functional programming, but it's short, natural, and fast: Drop[lst - RotateLeft[lst], -1] Rob Pratt Department of Operations Research The University of North Carolina at Chapel Hill rpratt at email.unc.edu http://www.unc.edu/~rpratt/ On Sat, 19 Aug 2000 Maarten.vanderBurgt at icos.be wrote: > Hallo, > > element. > I found two ways for doing this: > > lst = {a, b, c, d, e, f, g, h}; > > Table[lst[[i]] - lst[[i + 1]], {i, 1, Length[lst] - 1}] > {a - b, b - c, c - d, d - e, e - f, f - g, g - h} > > ListCorrelate[{1, -1}, lst] > {a - b, b - c, c - d, d - e, e - f, f - g, g - h} > > The first method is rather clumsy and the 2nd one is quite short, but not > really obvious. > Initally I was looking for a functional programming style routine. > Something like: (#[[i]]-#[[i-1]])&/@lst. > Who can tell me how to do this in a functional programming style? > > Thanks > > Maarten van der Burgt > Leuven, Belgium > > > >