# k times Fold with 3 changing extra variables

I need to generate a folded function like follows:

  f[ a[], b[], c[],
f[ a[], b[], c[],
f[ a[, b[], c[],
fstart
]
]
]


But instead of 3 times folded, I need it k-times folded.

a,b,c are lists containing numbers.

I tried to play around with Fold and FoldList and searched for similar questions, but I couldn't make it work :/.

Thanks for any help!

aa = Array[a, 5];
bb = Array[b, 5];
cc = Array[c, 5];

Fold[f[Sequence @@ #2, #] &, fstart, Transpose@{aa, bb, cc}]

   f[
a, b, c,
f[ a, b, c,
f[ a, b, c,
f[ a, b, c,
f[ a, b, c,
fstart
]
]
]
]
]

• Thank you very much! – Luke Sep 6 at 21:03

Alternatively, you can use Indexed and Fold over the indices:

Fold[
f[Indexed[a, #2], Indexed[b, #2], Indexed[c, #2], #1] &,
start,
Range
]

Out[] = f[Indexed[a, {4}], Indexed[b, {4}], Indexed[c, {4}],
f[Indexed[a, {3}], Indexed[b, {3}], Indexed[c, {3}],
f[Indexed[a, {2}], Indexed[b, {2}], Indexed[c, {2}],
f[Indexed[a, {1}], Indexed[b, {1}], Indexed[c, {1}], start]]]]

abc = {a, b, c};
k = 5;

Fold[f[## & @@ Through@abc@#2, #] &, fstart, Range @ k]


f[a, b, c, f[a, b, c, f[a, b, c, f[a, b, c, f[a, b, c, fstart]]]]]

Make it a function with 4 arguments:

ClearAll[fOLD]
fOLD[f_, lst_, strt_, k_] := Fold[f[## & @@ Through@lst@#2, #] &, strt, Range@k];


Examples:

fOLD[f, {a, b, c}, fstart, 5]


same result

fOLD[g, {x, y, z, w}, u0, 3]


g[x, y, z, w, g[x, y, z, w, g[x, y, z, w, u0]]]