带箭头的 OCaml 函数类型 ->
OCaml function type with arrows ->
我正在使用 OCaml StringMap.add 函数如下:
let rec makecount l = function
[] -> mymap
| s::tl ->
if (not (StringMap.mem s mymap)) then
StringMap.add s 1 makecount(tl)
else
StringMap.add s ((StringMap.find s mymap) + 1) makecount(tl)
我得到错误,指的是 StringMap.add s 1 makecount(List.tl l):
Error: This function has type StringMap.key -> 'a -> 'a StringMap.t -> 'a StringMap.t
It is applied to too many arguments; maybe you forgot a `;'.
首先,谁能解释一下函数类型的格式。那些箭头是什么意思?如果有人也能找到错误,那就太好了。
function
[] -> ...
| s::tl -> ...
是一个匿名函数,它将其参数与 []
和 s::tl
相匹配,并执行 "fits" 的第一个分支。 []
只会匹配一个空列表; s::tl
匹配任何包含至少一个元素 (s
) 的列表,其中 tl
是尾部 - 砍掉头部后得到的列表。
您将这个带参数的匿名函数分配给 makecount l
;意思是 makecount
本身现在需要两个参数(你永远不会再使用 l
)。解决方案很简单:只需删除 l
,并让 makecount
成为一个接受一个参数的函数(如上)。
如果你写:StringMap.add s 1 (makecount (List.tl l)) 会怎样?
发生的情况是 makecount 被视为代码中的参数,而您想要 makecount (List.tl l).
的结果
箭头表示 String.Map 需要 3 个参数(分别是类型 StringMap.key 、 'a 、 'a StringMap.t)和 returns 类型 ' 的结果StringMap.t.
我正在使用 OCaml StringMap.add 函数如下:
let rec makecount l = function
[] -> mymap
| s::tl ->
if (not (StringMap.mem s mymap)) then
StringMap.add s 1 makecount(tl)
else
StringMap.add s ((StringMap.find s mymap) + 1) makecount(tl)
我得到错误,指的是 StringMap.add s 1 makecount(List.tl l):
Error: This function has type StringMap.key -> 'a -> 'a StringMap.t -> 'a StringMap.t
It is applied to too many arguments; maybe you forgot a `;'.
首先,谁能解释一下函数类型的格式。那些箭头是什么意思?如果有人也能找到错误,那就太好了。
function
[] -> ...
| s::tl -> ...
是一个匿名函数,它将其参数与 []
和 s::tl
相匹配,并执行 "fits" 的第一个分支。 []
只会匹配一个空列表; s::tl
匹配任何包含至少一个元素 (s
) 的列表,其中 tl
是尾部 - 砍掉头部后得到的列表。
您将这个带参数的匿名函数分配给 makecount l
;意思是 makecount
本身现在需要两个参数(你永远不会再使用 l
)。解决方案很简单:只需删除 l
,并让 makecount
成为一个接受一个参数的函数(如上)。
如果你写:StringMap.add s 1 (makecount (List.tl l)) 会怎样? 发生的情况是 makecount 被视为代码中的参数,而您想要 makecount (List.tl l).
的结果箭头表示 String.Map 需要 3 个参数(分别是类型 StringMap.key 、 'a 、 'a StringMap.t)和 returns 类型 ' 的结果StringMap.t.