Last active
April 14, 2019 18:40
-
-
Save shintakezou/a5aa8743f09e8a317395b890bfc6712c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Counter | |
main = do print $ countUnique [ Element "zak" 2 0, | |
Element "mac" 3 1, | |
Element "abc" 4 2, | |
Element "zak" 5 0 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment