1

我对 OCaml 很陌生,正在尝试将 StringMap 转换为 OCaml 中的 List。

该地图是从之前的列表中生成的。

let map = List.fold_left(<SOME CODE HERE, WHICH I AM OMITTING>
    ) StringMap.empty
    in StringMap.fold(fun w c newlist -> (c,w)::newlist) map[]

上面代码的最后一行给了我以下错误: This expression has type StringMap.key list -> int StringMap.t but an expression is expected to type 'a StringMap.t = 'a Map.Make(String).t

请注意:此代码输入到 ocamllex 文件 (.mll) 中,当我尝试执行生成的词法分析器 (.ml) 文件时出现此错误。

为什么我会收到此错误?如何让我的代码工作?

谢谢!

4

2 回答 2

2

StringMap.bindings将返回对列表(key, value)

于 2014-07-21T08:36:11.323 回答
1

The error is telling you that the map value has type StringMap.key list -> int StringMap.t, which means that it's a function, not a map as you expected it. Furthermore, the function signature tells you what was missing in the previous expression to get a int StringMap.t as you expected: you need to add a parameter to the call to List.fold_left, of type StringMap.key list, which I suppose is a string list:

let map = List.fold_left(<SOME CODE HERE, WHICH I AM OMITTING>
) StringMap.empty string_list

Where string_list is the missing parameter: the list of keys used to build your map.

于 2014-07-18T22:34:05.377 回答