Skip to content

Instantly share code, notes, and snippets.

@lazyvalue
Created October 2, 2014 21:50
Show Gist options
  • Save lazyvalue/1e26f849be0c06d09e59 to your computer and use it in GitHub Desktop.
Save lazyvalue/1e26f849be0c06d09e59 to your computer and use it in GitHub Desktop.
import Data.List
mylst = [(1,3), (2,4), (5,6), (5,7), (6,8)]
findOverlaps :: [(Int,Int)] -> [(Int,Int)]
findOverlaps xs =
let sorted = sortBy (\x y -> if ((fst x) < (fst y)) then LT else GT) xs
foldF acum elem =
let (doneLst, (blockMin,blockMax)) = acum
(elemMin, elemMax) = elem
in if elemMin > blockMax then
((blockMin,blockMax) : doneLst, elem)
else
(doneLst, (blockMin,elemMax))
folded = foldl' foldF ([], head sorted) $ tail sorted
in reverse $ snd folded : fst folded
main = do
putStrLn $ show $ findOverlaps mylst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment