Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List (intercalate)
  2.  
  3. data Interval = Interval Int Int
  4.  
  5. instance Show Interval where
  6.     show (Interval start end)
  7.         | start == end = show start
  8.         | otherwise    = show start ++ "-" ++ show end
  9.  
  10. singletonInterval :: Int -> Interval
  11. singletonInterval x = Interval x x
  12.  
  13. convertToIntervals :: [Int] -> [Interval]
  14. convertToIntervals = foldr extendIntervals []
  15.     where extendIntervals val [] = [singletonInterval val]
  16.           extendIntervals val intervals
  17.               | firstIntervalStart - val == 1 = Interval val firstIntervalEnd : otherIntervals
  18.               | otherwise                     = singletonInterval val : intervals
  19.               where Interval firstIntervalStart firstIntervalEnd = head intervals
  20.                     otherIntervals                               = tail intervals
  21. main = do
  22.     input <- getContents
  23.     putStrLn $ intercalate "," . map show . convertToIntervals . map read . words $ input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement