Re: generalized foldlist problem

• To: mathgroup at smc.vnet.net
• Subject: [mg69085] Re: [mg69057] generalized foldlist problem
• From: Adriano Pascoletti <pascolet at dimi.uniud.it>
• Date: Wed, 30 Aug 2006 06:32:08 -0400 (EDT)
• References: <200608290725.DAA28981@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On 29 ago 2006, at 09:25, Arkadiusz Majka wrote:

> DearAll,
>
>
> 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
>

As the index i runs from 1 to the length of list1 generate
a table with list2[[i]] copies of list1[[i]] prepended with
i-1 0's, and right padded (with 0's) to the length of list1:

In[1]:=
list1 = {a, b, c, d, e};
list2 = {3, 2, 5, 1, 6};
{list2[[#1]]}]], Length[list1]] & ) /@
Range[Length[list1]]

Out[3]=
{{a, a, a, 0, 0}, {0, b, b, 0, 0}, {0, 0, c, c, c},
{0, 0, 0, d, 0}, {0, 0, 0, 0, e}}

In[4]:=
Table[1, {Length[list1]}] .
{list2[[#1]]}]], Length[list1]] & ) /@
Range[Length[list1]]

Out[4]=
{a, a + b, a + b + c, c + d, c + e}

Hope it helps