I have this definition of fold left
let rec fold_left f lst u = match lst with
| [] -> u
|(h::t) -> fold_left f t ( f h u)
I have to define reverse using the fold_left above. I currently have
let reverse l1 = fold_left (fun x y -> y::x) l1 []
but I keep getting this error
Error: This expression has type 'a list
but an expression was expected of type 'a
The type variable 'a occurs inside 'a list
What am I missing here?
You just have the accumulator and next item turned around (y::x
instead of x::y
). This works:
let reverse l1 = fold_left (fun x y -> x::y) l1 []