小编Beh*_*tx3的帖子

Haskell记录语法并从文件中读取.记录语法的字符串.***例外:Prelude.read:没有解析

我有下面的记录语法.

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)

syntax haskell exception record

3
推荐指数
1
解决办法
360
查看次数

标签 统计

exception ×1

haskell ×1

record ×1

syntax ×1