在Haskell中创建数据类型

时间:2014-12-01 14:08:51

标签: haskell types

我想创建一个新的数据类型,它接受Start或A元组的值。这就是我所拥有的:

type Coord = (Int, Int)
data Direction = N | E | S | W
                 deriving (Eq, Show, Read)


type Move = (Coord, Direction)
type BoardSpec = [(Coord, Bool)]

type GameTreeNode = (GameTree2, BoardSpec, Move)
data GameTree2 = Start | GameTreeNode deriving(Show)


blahblah :: GameTree2
blahblah = blahb

blahb :: GameTreeNode
blahb = (Start, testbo, ((1, 1), N))

然而,这给了我关于blahblah的错误

Couldn't match type `(GameTree2, Move)' with `GameTree2'
Expected type: GameTree2
  Actual type: GameTreeNode

1 个答案:

答案 0 :(得分:2)

<击>     数据GameTree2 =开始| GameTreeNode派生(显示)

扩展为:

data GameTree2 = Start | (GameTree2, BoardSpec, Move) deriving(Show) 

您无法使元组成为您的类型的有效值。

<击>


您需要将其包装在构造函数中:

data GameTree2 = Start | Node GameTreeNode deriving(Show)

其中Node是您为其选择的任何名称。