在 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/