我有我自己定义的这种类型:
data Item =
Book String String String Int -- Title, Author, Year, Qty
| Movie String String String Int -- Title, Director, Year, Qty
| CD String String String Int deriving Show -- Title, Artist, Year, Qty
我创建了一个空列表
all_Items = []
使用以下功能,我试图将 Item (Book) 类型的新书插入 all_Items
addBook all_Items = do
putStrLn "Enter the title of the book"
tit <- getLine
putStrLn "Enter the author of the book"
aut <- getLine
putStrLn "Enter the year this book was published"
yr <- getLine
putStrLn "Enter quantity of copies for this item in the inventory"
qty <- getLine
Book tit aut yr (read qty::Int):all_Items
return(all_Items)
但是,我收到此错误:
Couldn't match expected type `IO a0' with actual type `[a1]'
错误指向我使用 consing 运算符将新书添加到列表的行。我可以推测这是一个类型错误,但我无法弄清楚我做错了什么以及如何解决它。提前致谢!