我有下面的记录语法.
type Title = String
type Author = String
type Year = Int
type Fan = String
data Book = Book { bookTitle :: Title
, bookAuthor:: Author
, bookYear :: Year
, bookFans :: [Fan]
}
deriving (Show, Read)
type Database = [Book]
bookDatabase :: Database
bookDatabase = [Book "Harry Potter" "JK Rowling" 1997 ["Sarah","Dave"]]
Run Code Online (Sandbox Code Playgroud)
我想创建一个IO函数来读取books.txt文件并将其转换为类型数据库.我认为它需要与我下面的尝试相似.
main :: IO()
main = do fileContent <- readFile "books.txt";
let database = (read fileContent :: Database)
Run Code Online (Sandbox Code Playgroud)
books.txt文件的格式是什么?
下面是我目前的books.txt内容(与bookDatabase相同).
[Book "Harry Potter" "JK …Run Code Online (Sandbox Code Playgroud)