Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
data Item = I String deriving (Eq,Show)
data User = U String deriving (Eq,Show)
data Rating double = NoRating | R double deriving (Eq,Show)
--first function
occ a [] = False
occ a (x:xs) = if ( a == x ) then True
else occ a xs
dis :: Eq a => [a] -> [a]
dis [] = []
dis (x:xs) = if occ x xs then dis xs
else x:(dis xs)
--second function
fromRatingsToItems :: Eq a => [(b,a,c)] -> [a]
fromRatingsToItemshelper [] =[]
fromRatingsToItemshelper ((a,b,c):xs) = b: fromRatingsToItemshelper xs
fromRatingsToItems x = dis (fromRatingsToItemshelper x)
--third function
hasRating :: (Eq a, Eq b) => a -> b -> [(a,b,c)] -> Bool
hasRating _ _ [] = False
hasRating a b ((x,y,z):xs) = if a == x && b == y then True
else hasRating a b xs
--Fourth function
getRating :: (Eq a, Eq b) => a -> b -> [(a,b,c)] -> c
getRating _ _ [] = error"No given rating"
getRating a b ((x,y,z):xs) = if a == x && b == y then z
else getRating a b xs
--Fifth function **
-------------------------------------------------------------------------------------
formMatrixUser :: (Eq a, Eq b, Fractional c) => b -> [a] -> [(b,a,c)] -> [Rating c]
formMatrixUseh _ _ []=NoRating
formMatrixUseh u i ((ua,ia,r):xs)= if(u==ua && i==ia) then (R r)
else formMatrixUseh u i xs
formMatrixUser _ [] _=[]
formMatrixUser u (i:is) x=[formMatrixUseh u i x]++ formMatrixUser u is x
--sixth function
formMatrix :: (Eq a, Eq b, Fractional c) => [b] -> [a] -> [(b,a,c)] -> [[Rating c]]
formMatrix [] _ _ = []
formMatrix (a:b) x y = formMatrixUser a x y : formMatrix b x y
--7th function
numberRatingsGivenItem :: (Fractional a, Num b) => Int -> [[Rating a]] -> b
helper 0 (x:xs) = x
helper a (x:xs) = helper (a-1) xs
numberRatingsGivenItem _ [] = 0
numberRatingsGivenItem a (x:xs) = if helper a x == NoRating then numberRatingsGivenItem a xs
else 1+numberRatingsGivenItem a xs
--8th function
differeneRatings :: Fractional a => Rating a -> Rating a -> a
differeneRatings _ NoRating = 0.0
differeneRatings NoRating _ = 0.0
differeneRatings (R a) (R b) = a-b
--9th function
matrixPairs :: Num a => a -> [(a,a)]
helper2 a 0 = [(a,0)]
helper2 a b = [(a,b)] ++ helper2 a (b-1)
matrixPairs2 0 _= []
matrixPairs2 a b = helper2 (a-1) (b) ++ matrixPairs2 (a-1) b
matrixPairs 0 = []
matrixPairs b = reverse (matrixPairs2 b (b-1))
--10th function
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more