2013-11-28 29 views
0

錯誤的單子井字遊戲,我開始在Haskell實現井字遊戲:在Haskell

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 

這是什麼問題的原因,我該如何解決它?

回答

5

其實不是一個太神祕的信息。如果沒有Show情況下,添加一個:

data GameField = G [[Field]] deriving (Show) 
+1

對於加「派生秀」您GameField decleration ..這將簡單地加以解決記錄:大多數情況下,當GHC抱怨丟失的實例時,這實際上不是問題。 – leftaroundabout

2

你根本都忘了把它加,這樣

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]]