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?
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?
Take[{a, b, c, d}, #] & /@ Range[4]
(* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *)
Another possibility:
Reverse @ NestList[Most, {a,b,c,d}, 3]
{{a}, {a, b}, {a, b, c}, {a, b, c, d}}
Two ideas
f1[l_List] := Rest[FoldList[Append, {}, l]];
f2[l_List] := Table[Take[l, i], {i, Length[l]}]
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}}
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}}
myfun[x_?VectorQ] := ...– Bob Hanlon Jan 15 '18 at 02:35