Quantcast
Channel: Haskell Type into Data - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Haskell Type into Data

$
0
0

I am trying to change the Film type to a data type so it will make it easier to order and save and load to a file, I am having problems matching the functions up with the right arguments, it was a lot easier working with the types.

type Rating = (String, Int)--type Film = (String, String, Int, [Rating])data Film = Film String String Int [Rating]         deriving (Show,Ord,Eq, Read)testDatabase :: [Film]testDatabase = []testFilm = ("Test","Test",2012,[("Test",8),("Test",5)])saveToFile :: IO ()saveToFile = do    putStrLn "Enter the output filename: "    name <- getLine    writeFile name (show testDatabase)    putStrLn "Done"loadFromFile :: IO ()loadFromFile = do    putStrLn "Enter the input filename: "    name <- getLine    contents <- readFile name    let testDatabase = length contents `seq` (read contents :: [Film])    putStrLn formatDatabase    putStrLn "Done"average xs = realToFrac (sum xs) / genericLength xs addFilm :: String -> String -> Int -> [Film] -> [Film]addFilm title director year db = db ++ Film title director year []filmsByDirector :: String -> [Film]filmsByDirector name = filter (\(a,_,_,_) -> a == name) testDatabasefilmsByRating :: Float -> [Film]filmsByRating rating = filter (\(_,_,_,a) -> filmRating a > rating) testDatabasefilmsByYear :: Int -> [Film]filmsByYear year = filter (\(_,_,a,_) -> a == year) testDatabasefilmRating :: [(String,Int)] -> FloatfilmRating ratings = average (map snd ratings)formatString :: Film -> StringformatString (dir, film, year, rat) = printf "\n\nDirector: %s \nFilm Name: %s \nYear: %s \nRating: %4.2f" (show dir) (show film) (show year) (filmRating rat)formattedByYear :: Int -> StringformattedByYear year = concatMap formatString $ filmsByYear yearformattedByDirector :: String -> StringformattedByDirector dir = concatMap formatString $ filmsByDirector dirformatDatabase = concatMap formatString $ testDatabase

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images