簡體   English   中英

Haskell的Monadic TicTacToe錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM