Skip to content

Instantly share code, notes, and snippets.

/2.hs

Created December 6, 2016 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/55538d787adf8d9c2f1bf25dcff6442b to your computer and use it in GitHub Desktop.
Save anonymous/55538d787adf8d9c2f1bf25dcff6442b to your computer and use it in GitHub Desktop.
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