使用 fold left 定义反向

Define reverse using fold left

我有 fold left 的定义

let rec fold_left f lst u = match lst with
                            | [] -> u
                            |(h::t) -> fold_left f t ( f h u)

我必须使用上面的 fold_left 定义反向。我目前有

let reverse l1 = fold_left (fun x y -> y::x) l1 []

但我一直收到此错误

Error: This expression has type 'a list
       but an expression was expected of type 'a
       The type variable 'a occurs inside 'a list

我在这里错过了什么?

您只需将累加器和下一个项目转过来(y::x 而不是 x::y)。这有效:

let reverse l1 = fold_left (fun x y -> x::y) l1 []