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: [mg24934] Re: [mg24892] functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • From: Matt.Johnson at autolivasp.com
  • Date: Tue, 22 Aug 2000 16:22:43 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Here's one way:

In[80]:=
func[list_] := MapThread[#1 - #2 &, Transpose[Partition[list, 2, 1]]]
In[81]:=
func[lst]
Out[81]=
{a - b, b - c, c - d, d - e, e - f, f - g}

-matt





Maarten.vanderBurgt at icos.be on 08/19/2000 02:46:09 AM

cc:
Subject: [mg24934]  [mg24892] functional routine for {a, b, c, ...} -> {a - b, b - c,
      c - ...}


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: postscript font...
  • Next by Date: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • 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 - ...}