4

Assume there is a list:

List1={a,b,c,d};

Then I want to get these calculation:

Output1= a
Output2= a*b
Output3= a*b*c
Output4= a*b*c*d

Could you please help me with this problem? Thank you in advance.

Shellp
  • 523
  • 3
  • 13

3 Answers3

5
list1 = {a, b, c, d};

Rest[FoldList[Times, 1, list1]]

Scan[Print, %]

a
a b
a b c
a b c d

Karsten7
  • 27,448
  • 5
  • 73
  • 134
3

You can also use Accumulate

list1 = {a, b, c, d};

Times @@@ Accumulate[list1]
(* or Accumulate[list1] /. Plus -> Times *)

{a, a b, a b c, a b c d}

... or ReplaceList:

ReplaceList[list1, {x__, ___} :> Times[x]]

{a, a b, a b c, a b c d}

kglr
  • 394,356
  • 18
  • 477
  • 896
3

My version of Bob Hanlon's comment solution:

list1 = {a, b, c, d};

FoldList[Times, list1]

% // Column
{a, a b, a b c, a b c d}

a a b a b c a b c d

Reference: Shorter syntax for Fold and FoldList?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371