将数据定义为" [xxx]" ,我应该以什么形式在代码中使用它?

时间:2016-04-11 12:17:43

标签: haskell

数据定义如下:

data SquareWorld = SquareWorld { theAnt :: Ant, theWorld :: [Cell]}
deriving Show

data Coord = Coord {xCoord :: XCoord, yCoord :: YCoord}
deriving (Show, Eq)

data Cell = Cell {cellPosition :: Coord, cellState :: CellState}
deriving (Show, Eq)

data Ant = Ant { antPosition :: Coord
           , antOrientation :: Direction
           , antTransition :: [Transition SquareTurn]}
deriving (Show, Eq)

我写道:

changecolour :: SquareWorld -> SquareWorld 
changecolour squareWorld =
  let 
  old_cell = theWorld squareWorld
  old_cellstate = cellState old_cell
  new_cellstate =nextCellState old_cellstate 
  new_cell = old_cell { cellState = new_cellstate}
  newsquareWorld = squareWorld { theWorld = new_cell}
  in
  newsquareWorld

changedirection :: SquareWorld -> SquareWorld
changedirection squareWorld =
  let
  oldant = theAnt squareWorld
  old_cell = theWorld squareWorld
  old_cellstate = cellState old_cell
  olddirection = antOrientation oldant
  newant = case getStateColour old_cellstate of 
      1 -> oldant { antOrientation = succ' olddirection}
      0 -> oldant { antOrientation = pred' olddirection}
  newsquareWorld = squareWorld { theAnt = newant}
  in
  newsquareWorld

错误按摩:

src/StudentSources/LangtonsAnt.hs:152:31:
Couldn't match expected type ‘Cell’ with actual type ‘[Cell]’
In the first argument of ‘cellState’, namely ‘old_cell’
In the expression: cellState old_cell

src/StudentSources/LangtonsAnt.hs:154:16:
Couldn't match expected type ‘Cell’ with actual type ‘[Cell]’
In the expression: old_cell
In the expression: old_cell {cellState = new_cellstate}

src/StudentSources/LangtonsAnt.hs:155:47:
Couldn't match expected type ‘[Cell]’ with actual type ‘Cell’
In the ‘theWorld’ field of a record
In the expression: squareWorld {theWorld = new_cell}

src/StudentSources/LangtonsAnt.hs:164:31:
Couldn't match expected type ‘Cell’ with actual type ‘[Cell]’
In the first argument of ‘cellState’, namely ‘old_cell’
In the expression: cellState old_cell

我的语法一定有问题,但我不知道。 因为类型" Cell"实际上是[Cell]的类型,我应该把" []"在我的代码中?

我真的很困惑" []"在数据定义中。

0 个答案:

没有答案