Is there a clean way to fold a list but alternate functions used?
Say we call such function FoldAlternating
. Then for example,
The FoldAlternating[{Plus,Times}, {1, 5, 2, 3, 3, 2, 5}]]
would produce 235
,
Because it is equivalent to Times[Plus[Times[Plus[Times[Plus[1, 5], 2], 3], 3], 2], 5]
.
Edit: For $2$ functions f,g
and starting value v_: 1
, I can do it recursively:
fa[f_, g_, list_, v_: 1, i_: 1] :=
If[i + 1 == Length[list], f[g[v, list[[i]]], list[[i + 1]]],
If[i == Length[list], g[v, list[[i]]],
fa[f, g, list, f[g[v, list[[i]]], list[[i + 1]]], i + 2]]]
where fa[Times, Plus, {5, 2, 3, 3, 2, 5}]
gives the expected example from above.