Created
January 27, 2017 15:41
-
-
Save Osipion/85caaf3f6dec3121029578490d824869 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
-- Simple approach to grouping a list of items by being both contiguous and equal | |
-- The aim is to take a list of items, check each item sequentially to see if it is the same | |
-- as the last item. | |
-- Separates a list like this: [1,2,2,2,3,3,4,4,1,1,2,2] => [[1],[2,2,2],[3,3],[4,4],[1,1],[2,2]] | |
separate' :: Eq a => [a] -> Int -> [[a]] | |
separate' (x0 : xs@(x1 : _)) n | |
| x0 == x1 = separate' xs (n + 1) | |
| otherwise = (replicate n x0):(separate' xs 1) | |
separate' (x:_) n = [(replicate n x)] | |
separate :: Eq a => [a] -> [[a]] | |
separate items = separate' items 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment