Ocaml 如何使用自定义类型作为键创建Hashtbl?

Ocaml 如何使用自定义类型作为键创建Hashtbl?,ocaml,hashtable,Ocaml,Hashtable,我正在尝试创建一个Hashtbl,其中包含我编写的节点类型 type position = float * float type node = position * float 我想创建一个Hashtbl,其中节点作为指向浮点的键,并具有如下内容: [((node), float))] 这就是我迄今为止所尝试的: 模块HashtblNodes= 结构 类型t=节点 设相等=(=) 让hash=Hashtbl.hash 完;;; 以及: module HashNodes = Hashtbl.

我正在尝试创建一个Hashtbl,其中包含我编写的
节点
类型

type position = float * float
type node = position * float
我想创建一个Hashtbl,其中节点作为指向浮点的键,并具有如下内容:

[((node), float))]
这就是我迄今为止所尝试的:

模块HashtblNodes=
结构
类型t=节点
设相等=(=)
让hash=Hashtbl.hash
完;;;
以及:

module HashNodes = Hashtbl.Make(HashtblNodes);;
我不确定它是否是执行我之前解释的操作的正确实现,而且我不知道如何使用它创建表

我怎样才能做到这一点呢?

你的方法很有效(尽管你的问题是“你实际上不需要使用函子”)

从您在问题中的定义开始:

# let tbl = HashNodes.create 1 ;;
val tbl : '_weak2 HashNodes.t = <abstr>
# let node1 = ((1.0, 2.0), 3.0);;
val node1 : (float * float) * float = ((1., 2.), 3.)
# let node2 = ((-1.0, -2.0), -3.0);;
val node2 : (float * float) * float = ((-1., -2.), -3.)
# HashNodes.add tbl node1 100.0;;
- : unit = ()
# HashNodes.add tbl node2 200.0;;
- : unit = ()
# HashNodes.find tbl ((1.0, 2.0), 3.0) ;;
- : float = 100.
# 
#让tbl=HashNodes.create 1;;
val tbl:'_weak2hashnodes.t=
#设node1=((1.0,2.0,3.0);;
val节点1:(浮点*浮点)*浮点=((1,2.),3.)
#设node2=(-1.0,-2.0),-3.0);;
val节点2:(浮点*浮点)*浮点=(-1.,-2.,-3.)
#HashNodes.add tbl node1 100.0;;
-:单位=()
#HashNodes.add tbl node2 200.0;;
-:单位=()
#HashNodes.find tbl((1.0,2.0),3.0);;
-:float=100。
# 

您尝试过什么?这应该是完全直截了当的。@glennsl是的,对不起。到目前为止,我添加了我的尝试。你的尝试有什么问题?我觉得很好。但是,如果不打算为
equal
hash
提供不同的实现,那么实际上不需要使用functor。