[英]Error at Monadic TicTacToe in Haskell
我開始在Haskell中實現TicTacToe:
import Control.Monad.State
data Player = X | O
data Field = Player | I deriving (Eq, Show, Read)
data GameField = G [[Field]]
type GameState = State GameField ()
initGame :: GameState
initGame = do
put $ G [[I,I,I],[I,I,I],[I,I,I]]
action = do
initGame
test = execState action $ G [[I,I,I],[I,I,I],[I,I,I]]
當我執行“測試”時,出現以下錯誤:
No instance for (Show GameField) arising from a use of `print'
Possible fix: add an instance declaration for (Show GameField)
In a stmt of an interactive GHCi command: print it
此問題的原因是什么,如何解決?
其實不是太含糊的消息。 如果沒有Show
實例,請添加一個:
data GameField = G [[Field]] deriving (Show)
您只是忘了在您的GameField牌上添加“衍生節目”。這可以通過添加它來解決,就像這樣
import Control.Monad.State
data Player = X | O
data Field = Player | I deriving (Eq, Show, Read)
data GameField = G [[Field]]
deriving Show
type GameState = State GameField ()
initGame :: GameState
initGame = do
put $ G [[I,I,I],[I,I,I],[I,I,I]]
action = do
initGame
test = execState action $ G [[I,I,I],[I,I,I],[I,I,I]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.