您需要一个接受字符串并将其转换为格式化字符串的函数。实现是直接格式化。
formatString :: Film -> String
然后,您只需将此函数应用于Film
您感兴趣的每个(通过 a map
)并加入(concat
)最终字符串以获得结果。
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"