groupCount :: (Eq key) => [key] -> [(key, Int)]
groupCount = foldr groupCount' []
  where
    groupCount' :: (Eq key) => key -> [(key, Int)] -> [(key, Int)]
    groupCount' key [] = [(key, 1)]
    groupCount' key (next:collected) = if (fst next == key) then (fst next, snd next +1):collected else next:(groupCount' key collected)

{-
updateOrInsert :: (Eq key) => (value -> value -> value) -> [(key, value)] -> (key, value) -> [(key, value)]
updateOrInsert _ [] entry = [entry]
updateOrInsert updater (next:rest) entry =
  if (fst entry == fst next)
  then (fst next, updater (snd next) (snd entry)):rest
  else next:(updateOrInsert updater rest entry)
-}
groupCount :: (Eq key) => [key] -> [(key, Int)]
groupCount = foldr groupCount' []
  where
    groupCount' :: (Eq key) => key -> [(key, Int)] -> [(key, Int)]
    groupCount' key [] = [(key, 1)]
    groupCount' key (next:collected) = if (fst next == key) then (fst next, snd next +1):collected else next:(groupCount' key collected)

{-
updateOrInsert :: (Eq key) => (value -> value -> value) -> [(key, value)] -> (key, value) -> [(key, value)]
updateOrInsert _ [] entry = [entry]
updateOrInsert updater (next:rest) entry =
  if (fst entry == fst next)
  then (fst next, updater (snd next) (snd entry)):rest
  else next:(updateOrInsert updater rest entry)
-}
groupCount = foldr groupCount' []
  where
    groupCount' :: (Eq key) => key -> [(key, Int)] -> [(key, Int)]
    groupCount' key [] = [(key, 1)]
    groupCount' key (next:collected) = if (fst next == key) then (fst next, snd next +1):collected else next:(groupCount' key collected)

{-
updateOrInsert :: (Eq key) => (value -> value -> value) -> [(key, value)] -> (key, value) -> [(key, value)]
updateOrInsert _ [] entry = [entry]
updateOrInsert updater (next:rest) entry =
  if (fst entry == fst next)
  then (fst next, updater (snd next) (snd entry)):rest
  else next:(updateOrInsert updater rest entry)
-}