Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: generalized foldlist problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69151] Re: [mg69057] generalized foldlist problem
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 31 Aug 2006 04:39:06 -0400 (EDT)
  • References: <200608290725.DAA28981@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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}


  • Prev by Date: RE: transforming a rule of lists to a list of rules
  • Next by Date: Re: transforming a rule of lists to a list of rules
  • Previous by thread: Re: generalized foldlist problem
  • Next by thread: Re: generalized foldlist problem