Ocaml哈希表中的平等

pbp*_*pbp 6 ocaml equality hashtable

Ocaml中是否存在哈希表,==而不是=在测试键的相等性时使用?例如:

# type foo = A of int;;
# let a = A(1);;
# let b = A(1);;
# a == b;;
- : bool = false
# a = b;;
- : bool = true
# let h = Hashtbl.create 8;;
# Hashtbl.add h a 1;;
# Hashtbl.add h b 2;;
# Hashtbl.find h a;;
- : int = 2
# Hashtbl.find h b;;
- : int = 2
Run Code Online (Sandbox Code Playgroud)

我想要一个可以区分a和的哈希表b.那可能吗?

Tho*_*mas 11

您可以使用自定义哈希表:

module H = Hashtbl.Make(struct
  type t = foo
  let equal = (==)
  let hash = Hashtbl.hash
end)
Run Code Online (Sandbox Code Playgroud)

然后使用H而不是Hashtbl代码.