我有下一个代码:
module MakeLink (Key : Map.OrderedType) = struct
module Links = Map.Make (Key)
type 'a t =
{ links : 'a t Links.t;
value : 'a
}
type key_t = Key.t
let make value =
{ links = Links.empty;
value
}
let link linker ~to':linkable ~by:key =
{ linker with links =
Links.add key linkable linker.links
}
(* some functions for reading here *)
end
如何创建两个相互链接的链接?我试过了:
let join a ~with':b ~by:key =
let rec a' = link a ~to':b' ~by:key
and b' = link b ~to':a' ~by:(Key.opposite key) in
a'
但它看起来就像一只从自己的鸡蛋中孵化出来的母鸡。 我的问题是:如何使用(以 OCaml 或其他语言)在没有可变数据的情况下创建带循环的图形(例如:双向链表)?