recursion - OCaml:如何使用相同类型的 Set 参数创建归纳类型

标签 recursion types set ocaml

在 OCaml 中我可以定义以下类型:

type mytype = Base of int
            | Branch of (int * (collection -> collection))
and collection = mytype list

假设我根据每个构造函数的 int 值定义一个比较函数,如何将 collection 转换为 Set 而不是 list?

最佳答案

这是您需要使用递归模块的情况之一。事实上,您可以看到这是您在 documentation of the feature 中获得的实际示例。 。所以沿着这些思路应该做一些事情:

module rec Mytype : sig
  type t = Base ...
  val compare : t -> t -> int
end = struct
  type t = Base ...
  let compare v0 v1 = ...
end
and Collection : Set.S with type elt = Mytype.t 
               = Set.Make (Mytype)

关于recursion - OCaml:如何使用相同类型的 Set 参数创建归纳类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35999233/

相关文章:

javascript - 在 React 中呈现嵌套/线程评论

recursion - F# 中计算排列的递归函数存在类型不匹配错误

Python Quicksort 函数进入无限循环

python - 我的基于集合/字典的脚本很卡顿...字典重新哈希?

java - 为什么这会返回 boolean 值而不是长值?

java - 使用 Java 递归分解,处理 StackOverflow

f# - 无限类型(又名递归类型)在 F# 中是不可能的吗?

scala - 左和右类型相同的任何一种的标准特化

types - Trie 类型应该扩展哪个协议(protocol)?

javascript 转换设置为逗号分隔的字符串 - IE11