在Haskell中封装数据定义

Ext*_*der 1 syntax haskell types

我试图使用其他数据类型定义数据类型,如下所示:

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型.为什么会这样?

Don*_*art 7

您缺少数据构造函数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")
     ]
Run Code Online (Sandbox Code Playgroud)