MapThread a function with last result as input
Indeed, FoldList[]
would be the appropriate operation here:
FoldList[{2, -1, 3, -2}.Append[#2, #1] &, 0,
Transpose[{{7, 1, 5, 10, 10, 2, 5, 10, 10, 6},
{4, 1, 4, 2, 5, 2, 2, 4, 8, 10},
{2, 6, 1, 4, 6, 7, 4, 1, 2, 8}}]] // Rest
{16, -13, 35, -40, 113, -203, 426, -833, 1684, -3342}
Of course, modifying the snippet above to use fun[]
in the OP is not too hard to do (spot the difference!):
FoldList[fun[Append[#2, #1]] &, 0,
Transpose[{{7, 1, 5, 10, 10, 2, 5, 10, 10, 6},
{4, 1, 4, 2, 5, 2, 2, 4, 8, 10},
{2, 6, 1, 4, 6, 7, 4, 1, 2, 8}}]] // Rest
An alternative to FoldList
could be FoldPairList
:
FoldPairList[
Function[{prev, new}, {#, #}&@ ({2, -1, 3}.new - 2 prev) ],
0,
Transpose@sp
]
(* {16, -13, 35, -40, 113, -203, 426, -833, 1684, -3342} *)
In response to the request of keeping function fun
definition, you can modify the above to obtain the same result as follows:
ClearAll[fun]
fun[{x_, y_, z_, a_}] := 2 x - y + 3 z - 2 a
FoldPairList[
Function[{prev, new}, {#, #}&@ fun[{Sequence @@ new, prev}]],
0,
Transpose@sp
]
foldList = Rest @* FoldList[fun @* Flatten @* Reverse @* List] @* Prepend[0] @* Transpose;
Example:
sp = {{7, 1, 5, 10, 10, 2, 5, 10, 10, 6}, {4, 1, 4, 2, 5, 2, 2, 4, 8, 10},
{2, 6, 1, 4, 6, 7, 4, 1, 2, 8}};
foldList @ sp
{16, -13, 35, -40, 113, -203, 426, -833, 1684, -3342}
Also
foldList2 = Transpose /* Prepend[0] /* FoldList[fun @* Flatten @* Reverse @* List] /* Rest
foldList2 @ sp
{16, -13, 35, -40, 113, -203, 426, -833, 1684, -3342}