简体   繁体   English

Haskell的Monadic TicTacToe错误

[英]Error at Monadic TicTacToe in Haskell

I started to implement 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]]

When I execute "test" I get the following Error: 当我执行“测试”时,出现以下错误:

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

What is the cause of this problem, and how can I solve it? 此问题的原因是什么,如何解决?

Actually not a too cryptic message. 其实不是太含糊的消息。 If there is no Show instance, add one: 如果没有Show实例,请添加一个:

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

You simply forgot to add 'deriving show' on your GameField decleration.. This would simply be solved by adding it, like this 您只是忘了在您的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