I want to write a function that takes a List [a_n; a_n-1; ...; a_0] with an accumulator acc.
The function is supposed to calculate the sum of every element in the whole list raised to the i'th power. The function fold_left will give f an integer. The formula is acc + sum from i=0 to n of a_i ^ i. My problem is that in fold_left:
let fold_left f acc l =
match l with
| [] -> acc
| x::xs -> fold_left f (f x acc) xs
the accumulator always returns one integer -- so there's no reference for me to know what number the i'th element is.
So my question is how should I structure my f function.
f should be structured like this:
f a_0 (f a_1 (...(f a_n acc)...))
I tried an imperative approach by using a ref variable that stores the previous values that f has calculated thus far. But I'm sure there are better solutions to this problem...