Re: List difference using patterns and substitutions.
- To: mathgroup at smc.vnet.net
- Subject: [mg71317] Re: List difference using patterns and substitutions.
- From: bghiggins at ucdavis.edu
- Date: Wed, 15 Nov 2006 06:43:02 -0500 (EST)
- References: <ejc53g$6qf$1@smc.vnet.net>
Here are some ideas , though I am not sure using rules will be the most efficient l2 = Table[Random[Integer, {1, 10}], {20000}]; In[52]:= res1=Partition[l2,2,1]/.{x_Integer,y_Integer}->y-x;//Timing Out[52]= {0.168621 Second,Null} In[53]:= res2=Drop[RotateLeft[l2]-l2,-1];//Timing Out[53]= {0.002958 Second,Null} In[54]:= res3=ReplaceList[l2,{___,x_,y_,___}->y-x];//Timing Out[54]= {10.5521 Second,Null} In[50]:= res3==res2==res1 Out[50]= True Cheers, Brian Nacho 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.