I'm still trying to understand how fold_left
exactly works. Does it iterate through the list like List.iter
? Or is there just something else wrong with my code? I'm thinking that e is the element in the list (so it's a tuple) and fst e
takes the first element of the tuple and snd e
takes the second element in the tuple.
let rec pow x n =
if n < 0 then
0
else if n = 0 then
1
else
x * pow x (n - 1);;
let polynomial lst = function
| x -> List.fold_left (fun e -> (fst e) * (pow x (snd e))) 1 lst;;
lst is a list of tuples where each tuple has two integers and makes a polynomial function, so polynomial is supposed to return a function. So an example of what should happen is this
# let f = polynomial [3, 3; -2, 1; 5, 0];;
val f : int -> int = <fun>
# f 2;; (* f is the polynomial function f(x) = 3x^3 + (-2)x + 5 *)
- : int = 25
But I get this error message
"Error: This expression has type int but an expression was expected of type 'a -> int * int".