I have a problem with one code and hope that you can help me with that.
I have a list which looks like the following
List = {a, b, c, d, e}
the desired result is a new list with
{a, a*b, a*b*c, a*b*c*d, a*b*c*d*e}
Thanks in advance
Accumulate takes the sum of the terms: this can be changed to the product of by changing the Plus to Times:
list = {a, b, c, d, e};
Accumulate@list /. Plus -> Times
{a, a b, a b c, a b c d, a b c d e}
Block[{Plus = Times}, Accumulate@list]
if you don't want to use replacements.
$\endgroup$
Commented
Feb 1, 2020 at 5:58
Another way:
list = {a, b, c, d, e};
E^Accumulate[Log[list]]
Since Accumulate
equals Rest[FoldList[Plus,0,list]]
, So Foldlist
also works.
Another:
ReplaceList[{a, b, c, d,e}, { x__,___} :> Times@x ]
{a, a b, a b c, a b c d, a b c d e}
Comparing:
ReplaceList[{a, b, c, d,e}, { x__,___} :> Times@x ] == FoldList[Times] @ {a, b, c, d,e}
True
Here's an operator version of @JM's suggestion in the comments:
list = {a, b, c, d, e};
FoldList[Times] @ list
{a, a b, a b c, a b c d, a b c d e}
list = {a, b, c, d, e};
Using TimesBy
Module[{a = 1}, a *= # & /@ list]
{a, a b, a b c, a b c d, a b c d e}
Fold[Times, 1, list]
? AlsoList
is a built-in symbol, so don't use it as a variable. Stick to variables starting with lower-case letters. $\endgroup$FoldList[]
and notFold[]
; thanks @Darth! $\endgroup$FoldList[Times, {a, b, c, d,e}]
$\endgroup$