查看String Map中是否存在键

dl9*_*l96 3 ocaml dictionary

我目前正在使用Map仿函数来创建字符串映射:module StringMap = Map.Make(String).

然后我尝试将一组字符串映射插入'aStringMap 中的对象列表.检查密钥是否已存在,我正在执行以下操作:

match StringMap.find_opt key my_map with
  | None -> StringMap.add key [child] my_map
  | Some l -> StringMap.add key (child::l) my_map 
Run Code Online (Sandbox Code Playgroud)

但是,当我编译时,我收到一个错误,说绑定find_opt有一个未绑定的值,即使它在签名中定义:https://ocaml.org/learn/tutorials/map.html.

我也试过使用StringMap.mem key my_map,但得到以下错误:

Error: This expression has type string but an expression was expected of type
     'a StringMap.t =
       (StringMap.Key.t, 'a, StringMap.Key.comparator_witness)
       Base__Map.t
Run Code Online (Sandbox Code Playgroud)

我环顾四周,看看是否有输入错误或其他东西,但一直无法找到任何东西.关于我为什么会遇到这些错误的任何想法?

ivg*_*ivg 5

根据错误消息,您使用的是Base(或Core)库,它们是OCaml标准库的替代品,并具有不同的接口.特别是,接口中的find函数Map已经返回一个选项类型,因此没有find_opt函数.

也许,您已经使用了jbuilder教程中的一些示例来自动启用此库.

您可以通过打开兼容性模块切换到标准库或启用与vanilla OCaml标准库的兼容性Caml,例如,

open Caml

(* your code goes below *)
Run Code Online (Sandbox Code Playgroud)