6

I am very surprised not to find this function in the help (or in other questions on this site, which come close)

Is there a Mathematica function f such that

f[{a,b,c,d}] = {{a},{a,b},{a,b,c},{a,b,c,d}}

or must I construct it?

kglr
  • 394,356
  • 18
  • 477
  • 896
user2686410
  • 347
  • 1
  • 7

5 Answers5

8
Take[{a, b, c, d}, #] & /@ Range[4]

(* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
6

Another possibility:

Reverse @ NestList[Most, {a,b,c,d}, 3]

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
5

Two ideas

f1[l_List] := Rest[FoldList[Append, {}, l]];
f2[l_List] := Table[Take[l, i], {i, Length[l]}]
halirutan
  • 112,764
  • 7
  • 263
  • 474
5
ReplaceList[{a, b, c, d}, {x__, ___} :> {x}]

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

Table[{a, b, c, d}[[;; i]], {i, 4}]

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

{a, b, c, d}[[;; #]] & /@ Range@4

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

Partition[{a, b, c, d}, 4, 1, -1, {}]

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

Extract[{a, b, c, d}, List /@ Range @ Range @ 4]

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

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

For something different

f[x_List] := 
 LowerTriangularize@ConstantArray[x, Length[x]] /. 0 -> Nothing

f[{a, b, c, d}]

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

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158