我想使用Gloss从Haskell正在制作的游戏中读取文件。该文件为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
都被忽略了:
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"}]