Re: generalized foldlist problem
- To: mathgroup at smc.vnet.net
- Subject: [mg69169] Re: [mg69057] generalized foldlist problem
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 31 Aug 2006 04:40:10 -0400 (EDT)
- References: <200608290725.DAA28981@smc.vnet.net> <3028900F-A603-4119-86E7-AFBCCE5720F8@mimuw.edu.pl>
- Sender: owner-wri-mathgroup at wolfram.com
Sorry, there was a silly error in my first code. It worked only if the first symbol in the list was literally the letter a. The right version is, of course: GF[list1_, list2_] := Module[{f, g}, Evaluate[Map[f, list1]] = list2; g[ x_] := If[--f[x] < 0, 0, x]; FoldList[g /@ Plus[#1, #2] &, g[First[ list1]], Rest[list1]]] This time: list1 = {a, b, c, d, e}; list2 = {3, 2, 5, 1, 6}; GF[list1,list2] {a,a+b,a+b+c,c+d,c+e} Andrzej Kozlowski On 30 Aug 2006, at 23:35, Andrzej Kozlowski wrote: > > On 29 Aug 2006, at 08:25, 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 >> > > > Here is a solution which seems to me different from any of the ones > I have seen posted so far: > > GF[list1_, list2_] := Module[{f, g}, Evaluate[Map[f, list1]] = > list2; g[x_] := If[--f[x] < 0, 0, x]; > FoldList[g /@ Plus[#1, #2] &, g[a],Rest[list1]]] > > > GF[list1,list2] > > > {a,a+b,a+b+c,c+d,c+e}
- References:
- generalized foldlist problem
- From: "Arkadiusz Majka" <Arkadiusz.Majka@telekomunikacja.pl>
- generalized foldlist problem