-
-
Save anonymous/55538d787adf8d9c2f1bf25dcff6442b 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
import Data.List | |
groupByFirst :: (Eq a) => [(a, b)] -> [(a, [b])] | |
groupByFirst = map (trim . attach) . groupBy eq | |
where | |
eq (x, _) (y, _) = x == y | |
attach xs = (x, xs) | |
where (x, _):_ = xs | |
trim (a, abs) = (a, map snd abs) | |
main :: IO () | |
main = do | |
print $ groupByFirst [(0, 1), (0, 2), (1, 3), (1, 4)] | |
-- [(0,[1,2]),(1,[3,4])] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment