Compiler errors 使用List.fold\u left时出现错误类型

Compiler errors 使用List.fold\u left时出现错误类型,compiler-errors,ocaml,typeerror,Compiler Errors,Ocaml,Typeerror,这段代码有一个类型错误,如果你们能帮我,那就太好了 29 let function_1 func (accu: 'a list) b = if (func b) (* func is a function that takes in a value and returns a boolean*) 30 then accu::[] (* If func return false, I append an e

这段代码有一个类型错误,如果你们能帮我,那就太好了

29  let function_1 func (accu: 'a list) b = if (func b)    (* func is a function that takes in a value and returns a boolean*)
30                                        then accu::[]    (* If func return false, I append an empty list. It will create a list list, and it is my intention.*)
31                                        else accu::[b]   (* If func return true, I append the element b.*)
32
33  let function_2 func list =                             (* func is a function that takes in a value and returns a boolean. It is used in function_1*)
34           match list with
35           | [] -> [ [] ]
36           | x::rest -> List.fold_left function_1 func [x] rest
错误位于:

Line 36, characters 29-40
Error: This expression has type
          ('a list -> bool) -> 'a list -> 'a list -> 'a list list
       but an expression was expected of type
          ('a list -> bool) -> 'a list -> 'a list -> bool
       Type 'a list list is not compatible with type bool
如果可以的话,请给我解释,而不是实际代码。我更感兴趣的是如何调试它以及为什么它不工作


谢谢

列表的类型。左折叠是
('a->'b->'a)->'a->'b List->'a
,所以 传递给
fold_left
(在本例中为
function_1
)的函数的返回类型必须与其第一个参数相同。但是
函数1
的类型是
('a list->bool)->'a list->'a list->'a list->'a list>'a list list
而不是
('a list->bool)->'a list->('a list->bool)


我不知道你想要实现什么,所以我不能说太多。

一个好的开始是给你的函数命名一个比
function\u 1
function\u 2
func
更有意义的名字。这将极大地帮助其他人理解您试图做的事情,为了能够帮助您,注释函数的类型将对我们、编译器甚至您都有帮助。你甚至可以自己解决它,一旦你有了合适的类型,并且更好地了解它们不匹配的地方。我只是根据你的描述性注释发现了一个问题,这可能会被类型注释捕获:
accu::[]
并不意味着将空列表附加到
accu
列表中。这意味着您将
accu
元素(恰好是一个列表)附加到一个空列表中。结果是一个列表。@glennsl感谢您的帮助。我相信我的
函数_1
代码是正确的。我现在唯一的困难是,我无法找出导致这种类型错误的原因…如果您按照我的建议命名和注释函数的类型,您将通过澄清您的意图并允许我们帮助您来帮助我们和您自己。现在还不清楚你想要完成什么。