Ocaml 为高阶函数传递函数时调用递归函数

Ocaml 为高阶函数传递函数时调用递归函数,ocaml,higher-order-functions,Ocaml,Higher Order Functions,我想递归调用高阶函数。所以我有一个列表,我将这个列表传递给一个函数名funcName。但我需要传递一个函数。我想提出一个逻辑。我想检查元素是否为eric。如何使用递归函数获取乐趣I->语法 let aaa c : bool = let rec helper c = match c with |element(i) -> funcName (fun i->if (List.hd i)=eric then true else

我想递归调用高阶函数。所以我有一个列表,我将这个列表传递给一个函数名funcName。但我需要传递一个函数。我想提出一个逻辑。我想检查元素是否为eric。如何使用递归函数获取乐趣I->语法

let aaa c : bool = 
      let rec helper c =
        match c with 
        |element(i) -> funcName (fun i->if (List.hd i)=eric then true else 
                             //now i want to recursively call List.tl inside this inner function
      in
      helper c

当函数被定义为
fun
时,您似乎在问如何递归调用它,这是一个无名的问题

有很多方法可以做到这一点,但它们太复杂了,不值得这么做(在我看来)

您只需为无名函数命名即可:

let aaa c : bool =
    let rec helper c =
        let rec helper_helper i =
            if List.hd i = eric then true
            else (* Do your recursive calling of helper_helper *)
        in
        match c with
        | Element i -> funcName helper_helper
    in
    helper c

非常感谢你。但我有一个问题。所以在else括号内我想加上false。然后调用List.tl i上的递归函数。我只是不知道该把List.tl放在哪里。你介意给我一些建议吗?我遇到了错误的异常(失败“hd”),如果你知道列表不是空的,你只能调用
List.hd
。如果您不知道这一点,那么最好使用
匹配。(或者可以使用
if
测试非空列表。)