0
testDatabase :: [Film]
testDatabase = [("Director 1","Film 1",2012,[]),("Director 2","Film 2",2,[])]

filmsByDirector :: String -> [Film]
filmsByDirector name = filter (\(a,_,_,_) -> a == name) testDatabase

I am calling this from another function and I am trying to format the list so I can see an output like:

Director: Director 1
Film Name: Film 1
Year: 2012
Ratings:

Any help?

1
  • 3
    Write a function that takes a Film and returns a String, then map that over your list. The function can use unlines and show to generate strings from your data. Commented Apr 16, 2012 at 19:41

1 Answer 1

1

You want a function that takes a string and converts it into a formatted string. The implementation is a straight formatting.

formatString :: Film -> String

You then just need to apply this function to every Film you are interested in (via a map) and join (concat) that final string to get the result.

type Film = (String, String, Int, [Int])

testDatabase :: [Film]
testDatabase = [("Director 1","Film 1",2012,[]),("Director 2","Film 2",2,[])]

filmsByDirector :: String -> [Film]
filmsByDirector name = filter (\(a,_,_,_) -> a == name) testDatabase

formatString :: Film -> String
formatString (dir, film, year, rat) = "Director: " ++ (show dir) ++ "\nFilm Name: " ++ (show film) ++ "\nYear:" ++ (show year) ++ "\nRatings: " ++ concatMap (\r -> (show r) ++ " ") rat

formattedByDirector :: String -> String
formattedByDirector dir = concatMap formatString $ filmsByDirector dir

putStrLn $ formattedByDirector "Director 1"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.