迭代IO动作和懒惰

时间:2016-01-20 21:32:18

标签: haskell reactive-programming frp

我想编写Hangman游戏https://github.com/fokot/reactive-hangman/blob/master/src/Hangman.hs,将用户操作列表视为懒惰流。我的递归版本工作正常(在代码runGameRecursively(newGameState“secret”))

我被困在懒惰问题上

updateGameState :: GameState -> IO GameState
updateGameState gs = do
   l <- getALetter gs
   return $ updateState gs l

ff :: (a -> Bool) -> [IO a] -> IO a
ff f (i:is) = do
  res <- i
  if f res then return res else ff f is

runGameInfinite :: GameState -> IO ()
runGameInfinite gs =
  -- infinite lazy game loop
  let repl = tail $ iterate (\x -> x >>= updateGameState) (return gs) :: [IO GameState]
  in do
    endState <- ff gameEnded repl
    putStrLn $ showState endState

main = runGameInfinite (newGameState "car")

当您运行游戏时,repl中的每一步都需要重新评估所有以前的步骤,即使它们已经存在。我试着玩$!但是没有找到正确的答案。感谢

1 个答案:

答案 0 :(得分:2)

我认为使用iterate制作表面上纯粹的IO动作列表的方案是这里麻烦的根源。您的计划是按用户输入更新状态,但要将状态的连续性视为您可以像列表一样对待的流。如果我使用真正的iterateM来生成适当的流内容,那么事情就像你想要的一样。所以,如果我添加导入

import Streaming -- cabal install streaming
import qualified Streaming.Prelude as S

并且在您的主要定义之后写下类似

的内容
runGameInfiniteStream gs =  S.print $ S.take 1 $ S.dropWhile (not . gameEnded) steps
  where
  steps :: Stream (Of GameState) IO ()
  steps = S.iterateM updateGameState (return gs)

main :: IO ()
main = runGameInfiniteStream (newGameState "car")

然后我得到

>>> main
You have 5 lifes. The word is "___"
Guess a letter: 
c
You have 5 lifes. The word is "c__"
Guess a letter: 
a
You have 5 lifes. The word is "ca_"
Guess a letter: 
r
GameState {secretWord = "car", lives = 5, guesses = "rac"}

我认为这正是您想要的程序,但使用适当的流概念,而不是以某种复杂的方式混合IO和列表。可以使用pipesconduit以及类似的软件包进行类似的操作。

(稍后补充:)

要流式传输到纯粹的Chars列表对应的状态(模拟来自用户输入的结果),您只需使用scan

pureSteps
   :: (Monad m) => GameState -> [Char] -> Stream (Of GameState) m ()
pureSteps gs chars = S.scan updateState gs id (S.each chars)

这与Prelude.scanl基本相同,也可以用(在纯情况下)查看更新:

>>> S.print $ pureSteps (newGameState "hi") "hxi"
GameState {secretWord = "hi", lives = 5, guesses = ""}
GameState {secretWord = "hi", lives = 5, guesses = "h"}
GameState {secretWord = "hi", lives = 4, guesses = "h"}
GameState {secretWord = "hi", lives = 4, guesses = "ih"}

>>> mapM_ print $ scanl updateState (newGameState "hi") "hxi"
GameState {secretWord = "hi", lives = 5, guesses = ""}
GameState {secretWord = "hi", lives = 5, guesses = "h"}
GameState {secretWord = "hi", lives = 4, guesses = "h"}
GameState {secretWord = "hi", lives = 4, guesses = "ih"}

查看最终获胜的&#39;状态,如果它存在,你可以写,例如

runPureInfinite
  :: Monad m => GameState -> [Char] -> m (Of [GameState] ())
runPureInfinite gs = S.toList . S.take 1 . S.dropWhile (not . gameEnded) . pureSteps gs

-- >>> S.print $ runPureInfinite (newGameState "car") "caxyzr"
-- [GameState {secretWord = "car", lives = 2, guesses = "rac"}] :> ()

等等。