我正在尝试使用其他数据类型来定义数据类型,如下所示:
data A = Something String | SomethingElse Int
data B = Another B | YetAnother A
data C = A | B
x :: [ C ]
x = [ YetAnother (SomethingElse 0), Something "Hello World" ]
但这给了我一个错误,说我在期待 B 型时不能拥有 A 型。这是为什么呢?
您缺少C
.
data A = Something String
| SomethingElse Int
data B = Another B
| YetAnother A
data C = C0 A
| C1 B
x :: [ C ]
x = [ C1 (YetAnother (SomethingElse 0))
, C0 (Something "Hello World")
]
A
和B
indata C = A | B
是新数据构造函数的声明,而不是对现有类型的引用A
和B
. (构造函数在声明中不是可选的data
。)