MathGroup Archive 2000

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

Search the Archive

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


Hello

Maarten.vanderBurgt at icos.be schrieb in Nachricht
<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?


The Scan and Map operations can only use funtions of single list elements.
If you want functions of more than one element you need a specially prepared
list or an operation applied to the hole list.
The straight forward way for the 'derviative' is
  lst-RotateLeft[lst]
or
  #-RotateLeft[#]&[l]
(Maybe you want to remove the last element)


Marcel



  • Prev by Date: Undocumented Features In 4.0
  • Next by Date: Re: How to avoid complex exponents?
  • Previous by thread: Re: functional routine for {a, b, c, ...} -> {a - b, b - c, c - ...}
  • Next by thread: Initializing subscripted variables