I am very new to OCaml and am trying to convert a StringMap to a List in OCaml.
The map was generated from a list previously.
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[]
The last line in the code above gives me the following error: This expression has type StringMap.key list -> int StringMap.t but an expression was expected of type 'a StringMap.t = 'a Map.Make(String).t
Please note: This code is typed into an ocamllex file (.mll) and I get this error when I try to execute the lexical analyser (.ml) file generated.
Why am I getting this error? How do I get my code to work?
Thanks!
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.