我在读取文件并将其加载到类型 Film 的列表中时遇到问题:
testDatabase :: [Film]
testDatabase = [(Film "Ridely Scott" "Test" 2012 [("Zoe",2),("Test",1)]),(Film "Test1" "Test1" 2012 [("Test",4),("Test",5)])]
loadFromFile :: IO ()
loadFromFile = do
putStrLn "Enter the input filename: "
name <- getLine
contents <- readFile name
let testDatabase = length contents `seq` (read contents :: [Film])
putStrLn formatDatabase
putStrLn "Done"
Haskell 使用以下方法保存了文本文件:
saveToFile :: IO ()
saveToFile = do
putStrLn "Enter the output filename: "
name <- getLine
writeFile name (show testDatabase)
putStrLn "Done"
它的格式正确,它显示了大约 30 个条目,但是当调用加载方法时,我得到了运行时 testDatabase 中的数据。