Last active
June 19, 2020 00:11
-
-
Save lumie1337/e653a16dcf53ec3c0abb0b92298da165 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 (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