简体   繁体   中英

Error at Monadic TicTacToe in Haskell

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

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM