Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*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 2000

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

Search the Archive

Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24933] Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • From: "Paul R. Wellin" <wellin at wolfram.com>
  • Date: Tue, 22 Aug 2000 16:22:43 -0400 (EDT)
  • References: <8nli50$9fa@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Although I don't think it is any more intuitive than what you have below,
here is a functional approach:

In[1]:= lis = {a, b, c, d, e, f, g, h};

In[2]:= p = Partition[lis, 2, 1]
Out[2]= {{a, b}, {b, c}, {c, d}, {d, e}, {e, f}, {f, g}, {g, h}}

In[3]:= Apply[Subtract, p, 2]
Out[3]= {a - b, b - c, c - d, d - e, e - f, f - g, g - h}


Here it is in one step:

In[4]:= Apply[Subtract, Partition[lis, 2, 1], 2]
Out[4]= {a - b, b - c, c - d, d - e, e - f, f - g, g - h}

Paul Wellin
Wolfram Research, Inc.


<Maarten.vanderBurgt at icos.be> wrote in message
news:8nli50$9fa at smc.vnet.net...
> 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
>
>
>



  • Prev by Date: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • Next by Date: Re: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...a}
  • Previous by thread: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • Next by thread: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}