haskell - 并非 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"}]
解决方案
推荐阅读
- reactjs - 页面刷新后useState值变为未定义
- c# - wifi断开/重新连接后服务发现死机
- c# - JsonConvert.DeserializeObject 失败并出现 errorm_memberName
- swift - 对 UIImage 数组执行 remove(at:[index]) 会导致内存泄漏
- java - 我的程序中的 InputMismatchException 处理?- 爪哇
- python - 在 Python 中更新 DynomoDB 中的嵌套 JSON 结构
- php - 如何(数学上)收集任何进入数据库的数字
- html - 为什么第一节 CSS 类需要 calc(100% - 70px) 而不是 calc(100% - 75px) 在 CSS 和 HTML 中没有滚动条?
- javascript - Uncaught SyntaxError: Unexpected token ) 不确定我做错了什么?
- kotlin - 如何使用 Kotlin 协程正确地进行阻塞服务调用?