首页 > 解决方案 > 并非 do 块中的所有代码似乎都已执行

问题描述

我想从我在 Haskell 中使用 Gloss 制作的游戏中读取文件。该文件为 JSON 格式,并且 JSON 已正确加载,但随后它似乎被删除或其他东西。在下面的代码中,putStr (show highScores2)被忽略了,putStr第二个代码段中的两种情况也是如此。

为什么没有执行案例中的代码,这与惰性评估有关吗?(或者它是否被执行?)我不明白如何,因为这段代码对于程序的其余部分是必需的。

我的 JSON: [{"score":345,"date":"timeString"}]

我的代码:

main :: IO ()
main = do
        gen <- getStdGen
        highScores <- loadHighScores
        let highScores2 = highScores ++ [Entry 895 "testingtesting"]
        putStr (show highScores2)
        playIO
              FullScreen         -- Fullscreen
              black              -- Background color
              60                 -- Frames per second
              (initialState gen highScores2) -- Initial state
              view               -- View function
              input              -- Event function
              step               -- Step function

文件处理代码,在这个段B.putStr json中确实在屏幕上打印了一些东西,所以我知道它读取 JSON,但是putStr在 case 段中两个 s 都被忽略了:

  loadHighScores :: IO [HighScoreEntry]
  loadHighScores = do
                    json <- B.readFile highScorePath --Does not check for file does not exist
                    B.putStr json
                    -- let d = eitherDecode json :: Either String [HighScoreEntry]
                    -- case d of
                    --       Left err -> return []
                    --       Right hS  -> putStr "EUI" >> return hS
                    let d = decode json
                    case d of
                      Just hS -> (putStr $ "second - " ++ show hS) >> return hS
                      otherwise -> (putStr "whyyy") >> return []

输出:

[{"score":345,"date":"timeString"}]

标签: haskellinput

解决方案


推荐阅读