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
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI 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$
– NonDairyNeutrino
Feb 1 '20 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.
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}
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
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$ – J. M.'s ennui♦ Jan 31 '20 at 16:11FoldList[]
and notFold[]
; thanks @Darth! $\endgroup$ – J. M.'s ennui♦ Jan 31 '20 at 16:17FoldList[Times, {a, b, c, d,e}]
$\endgroup$ – A little mouse on the pampas Feb 1 '20 at 5:25