Skip to content

Instantly share code, notes, and snippets.

@lumie1337
Last active June 19, 2020 00:11
Show Gist options
  • Save lumie1337/e653a16dcf53ec3c0abb0b92298da165 to your computer and use it in GitHub Desktop.
Save lumie1337/e653a16dcf53ec3c0abb0b92298da165 to your computer and use it in GitHub Desktop.
import Data.List (intercalate)
consecutivesStrings :: [Int] -> String
consecutivesStrings = intercalate "," . map f . groupBy' (\a b -> a + 1 == b)
where
f [x] = show x
f xs = "[" ++ show (head xs) ++ "-" ++ show (last xs) ++ "]"
groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' p (x : xs) = go [x] xs
where
go (x : xs) (y : zs) | p x y = go (y : x : xs) zs
go g (y : zs) = reverse g : go [y] zs
go g [] = [reverse g]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment