MathGroup Archive 2006

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

Search the Archive

Re: List difference using patterns and substitutions.


a={1,2,3,5,5};

Rest[a]-Most[a]

{1,1,2,0}

Most is available in version 5 and later.

Rest[a]-Drop[a,-1]

{1,1,2,0}

ListConvolve[{1,-1},a]

{1,1,2,0}

Partition[a,2,1]/.{x_,y_}:>y-x

{1,1,2,0}

Thread[{Rest[a],Most[a]}]/.{x_,y_}:>x-y

{1,1,2,0}


Bob Hanlon

---- Nacho <ncc1701zzz at gmail.com> wrote: 
> Hello.
> 
> I'm trying to figure how can I build a difference list from another
> using only patterns and rule substitutions.
> 
> The idea is to get from a list, another, one element shorter, where
> each value is the substraction of two consecutive elements in the
> original list, that is, from {1,2,3,5,5} get {1,1,2,0}.
> 
> I've been thinking about it for a while, and I know several methods
> using a more traditional programming style (with For, for example), but
> I have no idea if it is possible to make it simple and fast with rule
> substitutions.
> 
> Any idea? 
> 
> Thanks in advance.
> 


  • Prev by Date: Re: List difference using patterns and substitutions.
  • Next by Date: Re: FindUnknowns[ ] (Mathematica wish list)
  • Previous by thread: Re: List difference using patterns and substitutions.
  • Next by thread: Re: List difference using patterns and substitutions.