haskell - OCaml 中的相互递归类型

标签 haskell recursion ocaml algebraic-data-types mutual-recursion

在 Haskell 中,您可以执行以下操作:

Prelude> data Foo = Foo Bar; data Bar = Bar Foo

你怎么能在 OCaml 中做同样的事情?我试过:

                    ___
# type foo = Foo of bar;; type bar = Bar of foo;;
Error: Unbound type constructor bar

甚至可以在 OCaml 中定义相互递归的数据类型吗?如果不是,那为什么?

比较数据定义以让表达式:相互递归的数据类型对应于使用 let rec (或者更合适的 type rec 想要一个更好的短语)。能够定义相互递归的数据类型有什么好处?我的 foobar 示例很简单。你能想到相互递归数据类型的任何重要用途吗?

最佳答案

使用 and

type foo = Foo of bar
 and bar = Bar of foo

关于haskell - OCaml 中的相互递归类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29471873/

相关文章:

haskell - 使用 OCaml 和 Haskell 制作独立的顶层

integer - Reason ML 中的整数类型和模块是什么?

Haskell Records-使用字符串字段名设置/获取

haskell - 如何将 `bound` 用于非 monad?

Haskell Double 除以 Int

Haskell 的 Scrap Your Boilerplate (SYB) - 仅应用一次转换,而不是到处应用转换

c++ - OpenMP 中 omp_set_max_active_levels 的最佳值是多少?

python - 确保一个 Action 在递归调用期间只发生一次

java - 使用递归对数字 ArrayList 进行排列

OCaml 顶层不解释注释