在Haskell中将构造函数和Data作为参数传递

时间:2016-02-07 19:38:55

标签: haskell ghci haskell-prelude

我正在尝试为我正在处理的游戏构建一个小错误检查器,我不确定如何将数据类型作为参数传递。这是我想要做的 这是我的数据参数

的文件
-- | Represents the type of move played in a 'GameState'.
data Played = Played ((Int, Int), (Int, Int)) -- ^ A "normal" move.
            | Passed                          -- ^ A (legal) pass.
            | Goofed ((Int, Int), (Int, Int)) -- ^ An illegal move, penalty applied.
            | Init                            -- ^ No one has moved yet.
            | UpgradedPawn2Knight (Int,Int)   -- ^ A pawn reached the other side when <2 knights.
            | PlacedPawn ((Int, Int), (Int, Int)) -- ^ A pawn that's been placed in any empty space after having reached the far end of the board.
            | BadPlacedPawn ((Int, Int), (Int, Int)) -- ^ A strategy has attempted to do a pawn placement, but chose an invalid location.

这是我的代码试图实现基于这些游戏状态的函数

prevPenalty :: Played -> Int
prevPenalty Init = 0
prevPenalty Goofed ((a, b),(c, d)) = 1
prevPenalty Passed ((a, b),(c, d)) = 0


currentPenalty :: Played -> Int
currentPenalty Goofed ((a, b),(c, d)) = 1 + prevPenalty
currentPenalty Passed ((a, b),(c, d)) = 0 + prevPenalty

但是,在编译时会出现此错误

Penalty.hs:22:1:
    Equations for ‘prevPenalty’ have different numbers of arguments
      Penalty.hs:22:1-20
      Penalty.hs:23:1-38

Penalty.hs:28:1:
    Couldn't match expected type ‘t0 -> t1’ with actual type ‘Int’
    The equation(s) for ‘currentPenalty’ have two arguments,
    but its type ‘Played -> Int’ has only one

我无法弄清楚如何解决这个问题......任何人都有任何建议吗?

0 个答案:

没有答案