How to make FoldListIndexed ie FoldList with an index or iterator?
foldIndexedList = Module[{i = 1, f = #}, FoldList[f[##, i++] &, ##2]] &;
foldIndexedList[f, x, {a, b, c, d}]
{x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}
foldIndexedList2 = Module[{f = #},
FoldList[f[#, ## & @@ #2] &, #2, MapIndexed[{#, #2[[1]]} &]@#3]] &;
foldIndexedList2[f, x, {a, b, c, d}]
{x, f[x, a, 1], f[f[x, a, 1], b, 2], f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}
You can try
FoldListIndexed[f_, x_, lst_] :=
FoldList[
Function[{a, b}, f[a, Sequence @@ b]],
x,
Transpose[{lst, Range @ Length @ lst}]
]
and then
In[4]:= FoldListIndexed[f, x, {a, b, c, d}]
Out[4]= {x, f[x, a, 1], f[f[x, a, 1], b, 2],
f[f[f[x, a, 1], b, 2], c, 3], f[f[f[f[x, a, 1], b, 2], c, 3], d, 4]}