Instantly share code, notes, and snippets.

# shintakezou/counter.hs Last active Apr 14, 2019

 module Counter (Element(..), countUnique, countUnique2) where data Element = Element { s :: String, e1 :: Int, e2 :: Int } deriving (Show, Eq) countUnique :: [Element] -> Int countUnique [] = 0 countUnique (x:xs) | x `foundIn` xs = countUnique xs | otherwise = 1 + countUnique xs where foundIn :: Element -> [Element] -> Bool foundIn x [] = False foundIn x xs = any (== 0) \$ map (compare x) xs where compare :: Element -> Element -> Int compare a b | (e2 a) == (e2 b) = 0 | (e1 a) /= (e1 b) = (e1 b) - (e1 a) | otherwise = (e2 b) - (e2 a) countUnique2 :: [Element] -> Int countUnique2 [] = 0 countUnique2 (x:xs) = let foundIn :: Element -> [Element] -> Bool foundIn x [] = False foundIn x xs = any (== 0) \$ map (compare x) xs where compare :: Element -> Element -> Int compare a b | (e2 a) == (e2 b) = 0 | (e1 a) /= (e1 b) = (e1 b) - (e1 a) | otherwise = (e2 b) - (e2 a) in case () of _ | x `foundIn` xs -> countUnique2 xs | otherwise -> 1 + countUnique2 xs countUnique3 :: [Element] -> Int countUnique3 [] = 0 countUnique3 (x:xs) | foundIn xs = countUnique3 xs | otherwise = 1 + countUnique3 xs where foundIn :: [Element] -> Bool foundIn [] = False foundIn xs = any (== 0) \$ map (compare) xs where compare :: Element -> Int compare b | (e2 x) == (e2 b) = 0 | (e1 x) /= (e1 b) = (e1 b) - (e1 x) | otherwise = (e2 b) - (e2 x)
 import Counter main = do print \$ countUnique [ Element "zak" 2 0, Element "mac" 3 1, Element "abc" 4 2, Element "zak" 5 0 ]
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.