为什么我的F#map实现不能编译

我已经开始学习F#,并且我想用tail-recursion编写我自己的地图函数。 这是我的

let my_map ff list =
    let rec mapAcc ff_inner list_inner acc =
        match list_inner with
        | [] -> acc
        | front::rest -> mapAcc( ff_inner rest (ff_inner(front) :: acc) ) //error
    mapAcc ff list []

它会被这样调用:

let list = my_map (fun x -> x * 2) [1;2;3;4;5] // expect [2;4;6;8;10]

第二个条件中出现Type mismatch. Expecting a 'a but given a 'b list -> 'a -> 'a The resulting type would be infinite when unifying ''a' and ''b list -> 'a -> 'a'的编译错误消息Type mismatch. Expecting a 'a but given a 'b list -> 'a -> 'a The resulting type would be infinite when unifying ''a' and ''b list -> 'a -> 'a' Type mismatch. Expecting a 'a but given a 'b list -> 'a -> 'a The resulting type would be infinite when unifying ''a' and ''b list -> 'a -> 'a'

我不知道这个错误信息的含义。 我不确定如果我通过递归调用mapAcc来传递rest ,这是多么的无限。

注意:我意识到我正在向后重建列表。 我现在无视这一点。


当函数调用它自己时,请删除括号:

let my_map ff list =
    let rec mapAcc ff_inner list_inner acc =
        match list_inner with
        | [] -> acc
        | front::rest -> mapAcc ff_inner rest (ff_inner(front) :: acc)
    mapAcc ff list []

否则,其中包含的所有内容都将被解释为单个参数,而ff_inner作为函数调用,其余部分将作为参数进行解释。

链接地址: http://www.djcxy.com/p/80555.html

上一篇: Why doesn't my F# map implementation compile

下一篇: Best approach for designing F# libraries for use from both F# and C#