# k times Fold with 3 changing extra variables

I need to generate a folded function like follows:

  f[ a[[3]], b[[3]], c[[3]],
f[ a[[2]], b[[2]], c[[2]],
f[ a[[1], b[[1]], c[[1]],
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!

Does it fit your needs?

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

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

   f[
a[5], b[5], c[5],
f[ a[4], b[4], c[4],
f[ a[3], b[3], c[3],
f[ a[2], b[2], c[2],
f[ a[1], b[1], c[1],
fstart
]
]
]
]
]

• Thank you very much!
– Luke
Sep 6, 2019 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[4]
]

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[5], b[5], c[5], f[a[4], b[4], c[4], f[a[3], b[3], c[3], f[a[2], b[2], c[2], f[a[1], b[1], c[1], 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[3], y[3], z[3], w[3], g[x[2], y[2], z[2], w[2], g[x[1], y[1], z[1], w[1], u0]]]