The task in question allows a user to re-rate a film such that only the latest rating from the user should remain in the database. Initially I solved this problem in a very basic manor?
This was my result:
type Rating = (String, Int)
type Film = (String, String, Int, [Rating])
--Example ("Blade Runner","Ridley Scott",1982, [("Amy",5),("Bill",8)])
where the functions:
--Find selected film, isolate into new List
getFilm :: String -> [Film] -> [(Film)]
getFilm search aFilm = [(title,dir,year,ratings) | (title,dir,year,ratings) <- aFilm, search == title]
--Update the new film and pull details for amendment, STORE
pullTitle :: [(Film)] -> String
pullTitle [(title,_,_,_)] = title
pullDirector :: [(Film)] -> String
pullDirector [(_,director,_,_)] = director
pullYear :: [(Film)] -> Int
pullYear [(_,_,year,_)] = year
pullRatings :: [(Film)] -> [Rating]
pullRatings [(_,_,_,rating)] = rating
newRatings :: [Rating] -> Rating -> [Rating]
newRatings oldRatings (newUser,newRate) = [(user,rate) | (user,rate) <- oldRatings, user /= newUser] ++ [(newUser,newRate)]
--Database without selected film
updateDatabase :: String -> [Film] -> [(Film)]
updateDatabase search aFilm = [(title,dir,year,ratings) | (title,dir,year,ratings) <- aFilm, search /= title]
--Adds new film to a database
updatedFilm :: String -> String -> Int -> [Rating] -> [Film] -> [Film]
updatedFilm name director yearOfRel ratings database = database ++ [(name,director,yearOfRel,ratings)]
--Add new film to updated Database
formulateFilm :: [(Film)] -> [(Film)] -> Rating -> [(Film)]
formulateFilm soloFilm updatedFilms updateRating = updatedFilm (pullTitle soloFilm) (pullDirector soloFilm) (pullYear soloFilm) (newRatings (pullRatings soloFilm) updateRating) updatedFilms
--Compiling to for finalcome
rateMovie :: String -> Rating -> [Film] -> [(Film)]
rateMovie mTitle newRating oldDatabase = formulateFilm (getFilm mTitle oldDatabase) (updateDatabase mTitle oldDatabase) newRating
I have changed the following functions to make use of filters:
getFilm :: String -> [Film]
getFilm search = filter(\(title,_,_,_) -> search == title) testDatabase
updateDatabase :: String -> [Film]
updateDatabase search = filter(\(title,_,_,_) -> search /= title) testDatabase
Could someone advise me on how I would use maps to get the selected films data from getFilm :: String -> [Film]
, similar to the (pull*) functions?