我在Haskell中定义新的数据类型时遇到了麻烦.
我正在尝试创建一个数据类型NumPair,它将是一个包含两个整数或整数和另一个整数的元组NumPair.
例如,(2, 2), (0, 5), (1, (2, 3)) and (0, (4, (3, 121)))一切都应该有效NumPairs.
这是我为了尝试这样做而编写的代码:
data NumPair = (Int, Int) | (Int, NumPair) deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这不起作用,我应该做什么呢,拜托?
我试图使用其他数据类型定义数据类型,如下所示:
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" ]
Run Code Online (Sandbox Code Playgroud)
但是这给了我一个错误,说我在期待B型时不能有A型.为什么会这样?