Created
November 12, 2010 21:02
-
-
Save hellertime/674679 to your computer and use it in GitHub Desktop.
compute the bounds of consecutive intervals along an enumeration
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
intervals :: (Eq a, Enum a) => [a] -> [(a, Maybe a)] | |
intervals [] = [] | |
intervals (x:[]) = [(x, Nothing)] | |
intervals (x:xs) = (x, lastSuccessor x x xs) : intervals (dropSuccessors x xs) | |
where | |
lastSuccessor :: (Eq a, Enum a) => a -> a -> [a] -> Maybe a | |
lastSuccessor l x [] | |
| l == x = Nothing | |
| otherwise = Just x | |
lastSuccessor l x (y:ys) | |
| succ x == y = lastSuccessor l y ys | |
| otherwise = lastSuccessor l x [] | |
dropSuccessors :: (Eq a, Enum a) => a -> [a] -> [a] | |
dropSuccessors x [] = [] | |
dropSuccessors x done@(y:ys) | |
| succ x == y = dropSuccessors y ys | |
| otherwise = done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see gist https://gist.github.com/674538 for a Python version of this