Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Osipion/85caaf3f6dec3121029578490d824869 to your computer and use it in GitHub Desktop.
Save Osipion/85caaf3f6dec3121029578490d824869 to your computer and use it in GitHub Desktop.
-- 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