Re: generalized foldlist problem
- To: mathgroup at smc.vnet.net
- Subject: [mg69093] Re: generalized foldlist problem
- From: "sashap" <pavlyk at gmail.com>
- Date: Wed, 30 Aug 2006 06:32:30 -0400 (EDT)
- References: <ed0rja$sll$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Arkadiusz, I think the following does what you wanted: In[5]:= GeneralizedFoldList[e1_List,e2_List]/;ArrayQ[e2,1,(IntegerQ[#]&&Positive[#])&]\ &&Length[e1]\[Equal]Length[e2]:=Module[{cntrs,fun}, cntrs=e2; fun[k_Integer]:=If[Positive[Extract[cntrs,k]], cntrs=ReplacePart[cntrs,(Extract[cntrs,k]-1),k];Extract[e1,k],0]; Rest[FoldList[Plus,0,Hold[fun]/@Range[Length[e1]]]]//ReleaseHold ] In[6]:= GeneralizedFoldList[{a,b,c,d,e},{3,2,5,1,6}] Out[6]= {a,a+b,a+b+c,c+d,c+e} Oleksandr Pavlyk Wolfram Research Arkadiusz Majka wrote: > DearAll, > > Please, help! > > I have two list > > list1={a,b,c,d,e} > list2={3,2,5,1,6} > > and I want to apply a modified version of FoldList to list1 in the > following way: list2 indicates that element a appears only 3 times (if > space enough) beginning from the beginning of the list , element b > appears 2 times, c - 5 times , etc. > > So the output should be > > GeneralizedFoldList[list1,list2]={a,a+b,a+b+c,c+d,c+e} > > Thanks for any hints, > > arek