Advertisement
Vladi1442

Untitled

Jun 4th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. main :: IO()
  4. main = do
  5.     print $ rf (Song "Mozart""The Marriage of Figaro Overture" 270) -- == "Summertime"
  6.     print $ rf (Song "Gershwin""Summertime" 300) -- == "Rhapsody in Blue"
  7.     print $ rf (Song "Gershwin""Rhapsody in Blue" 1100) -- == "Rhapsody in Blue"
  8.  
  9. type SongName = String
  10.  
  11. songs = [
  12.     (Song "Mozart""The Marriage of Figaro Overture" 270),
  13.     (Song "Gershwin""Summertime" 300),
  14.     (Song "Queen""Bohemian Rhapsody" 355),
  15.     (Song "Gershwin""Rhapsody in Blue" 1100)]
  16.  
  17. type AuthorName = String
  18. type SongLength = Int
  19.  
  20. data Song = Song AuthorName SongName SongLength -- that is the main line we need
  21.     deriving (Eq, Show)
  22.  
  23. data Playlist = Playlist [Song]
  24.  
  25. rf = recommender (Playlist songs)
  26.  
  27. -- aN = authorName, sN = songName, sL = songLength
  28. -- pl is a list, actually it represented PlayList
  29.  
  30. -- we are gonna split function in two parts
  31. -- in the first we need to have filter and dropWhile
  32. -- in the second part we are just gonna need filter
  33.  
  34. recommender :: Playlist -> (Song -> SongName)
  35. recommender (Playlist pl) = (\p@(Song aN sN sL) -> if null $ recommendALgorithm1 aN sL then if null $ recommendAlgorithm2 aN sL then p else head recommendAlgorithm2 aN sL else head $ recommendALgorithm1 aN sL)
  36.     where
  37.         -- here we need to use filter and dropWhile, and check if length is bigger, if it's bigger than we move on to another fucntion
  38.  
  39.         recommnedAlgorithm1 :: AuthorName -> Int ->
  40.         recommendAlgorithm1 aN l = filter (\(Song authorName sN sL) -> sL > l && aN == authorName) pl
  41.  
  42.         recommentAlgorithm2 :: AuthorName -> Int ->
  43.         recommendAlgorithm2 aN l = (\p@(Song authorName sN sL) -> if length (dropWhile (/= p) pl) < 1 then p else head $ tail recommendAlgorithm2 aN l)
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement