1

我无法使用在我的 main :: IO() 函数中返回 Either monad 的函数。我可以在不使用 Either 的情况下运行我的代码,但我现在使用 Either 来处理错误。我有以下代码:

parse :: Parser a -> String -> Either TypeRep a
parseFile :: String -> Either TypeRep Game

main :: IO()
main = do 
  content <- readFile "file.txt"
  let info = parseFile content
  case info of
    Left e -> error $ show e
    Right game -> let level = makeGame game
               Gloss.play ... level ... 

因此,如果我的解析函数刚刚返回 a,我的代码就能够执行 Gloss.play 等等。我如何使用 IO 在主中处理 Either。

4

1 回答 1

2

你很近。在您的Right game ->情况下,您想引入另一个do-block,例如:

main :: IO()
main = do 
  content <- readFile "file.txt"
  let info = parseFile content
  case info of
    Left e -> error $ show e
    Right game -> do
      let level = makeGame game
      Gloss.play ... level ... 
于 2020-11-28T21:30:06.747 回答