Consing lists with user-defined type in Haskell

Posted by user1319603 on Stack Overflow See other posts from Stack Overflow or by user1319603
Published on 2012-04-07T23:12:44Z Indexed on 2012/04/07 23:29 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

I have this type I defined myself:

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

I've created an empty list

all_Items = []

With the following function I am trying to insert a new book of type Item (Book) into the 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)

I however am receiving this error:

Couldn't match expected type `IO a0' with actual type `[a1]'

The error points to the line where I am using the consing operator to add the new book to the list. I can gather that it is a type error but I can't figure out what it is that I am doing wrong and how to fix it. Thanks in Advance!

© Stack Overflow or respective owner

Related posts about list

Related posts about haskell