Tom
Tom

Reputation: 51

How do i display my list as a string in haskell?

Here is the code i have used to build the list.

type Title = String
type Actor = [String]
type Year = Int
type Fans = [String]
type Film = (Title, Actor, Year, Fans)

type Database = [Film]  
filmDatabase :: Database
filmDatabase = [("Casino Royale", ["Daniel Craig", "Eva Green", "Judi Dench"], 2006, ["Garry", "Dave", "Zoe", "Kevin", "Emma"])...]

below is the code i Am using to try and display the list as the string.

titleAsString :: Title -> String
titleAsString = show

actorsAsString :: Actor -> String
actorsAsString = intercalate ", " . map show

yearAsString :: Year -> String
yearAsString = show

fansAsString :: Fans -> String
fansAsString = intercalate ", " . map show

filmsAsString :: [Film] -> String
filmsAsString (t, a, y, f) = unlines [titleAsString t, actorsAsString a, yearAsString y, fansAsString f]

filmsAsStringDB = unlines . map filmsAsString

here is the error message i get..

 Couldn't match type ‘(Title, Actor, Year, Fans)’ with ‘Char’
    Expected type: Title
      Actual type: [Film]
    In the first argument of ‘titleAsString’, namely ‘t’
    In the expression: titleAsString t

I think the problem is i haven't converted them all to a string but i'm not sure how to...

Upvotes: 0

Views: 123

Answers (2)

RasmusWL
RasmusWL

Reputation: 1593

You should do this instead

filmAsString :: Film -> String
filmAsString (t, a, y, f) = unlines [titleAsString t, actorsAsString a, yearAsString y, fansAsString f]

databaseAsString :: Database -> String
databaseAsString = unlines . map filmAsString

Upvotes: 0

jwodder
jwodder

Reputation: 57620

You've declared filmsAsString to be of type [Film] -> String, yet its argument is clearly just a Film.

Upvotes: 3

Related Questions