3

我正在尝试在 Haskell 中实现游戏。我有一个 GameState 类型,它管理诸如分数、玩家和回合之类的事情,其中​​回合是管理游戏细节的 RoundState 类型。玩游戏,我有一个函数

playGame :: (RandomGen g) => State (GameState g) (Player, Int)
playGame = do playRound
              winner <- checkForWinner
              case winner of 
                  Nothing -> playGame
                  Just x -> return x

在哪里

checkForWinner :: RandomGen g => State (GameState g) (Maybe (Player, Int))
playRound :: RandomGen (g) => State (GameState g) ()

但这并不是很有趣,因为没有 IO monad,我无法将任何内容输出到屏幕上。

如何在保持 playGame 递归的同时将此函数包装在 IO monad 中?

4

1 回答 1

7

目前你的单子只是状态,没有IO

State (GameState g)

你想要的是一个带有状态和 IO 的单子:

type Game g = StateT (GameState g) IO

现在您可以按照您的预期使用 monad

import Control.Monad.IO.Class (liftIO)

-- ...

playGame :: (RandomGen g) => Game g (Player, Int)
playGame = do
    liftIO $ putStrLn "Look, I have IO"
    winner <- checkForWinner
    ...
于 2013-11-02T22:56:12.003 回答