Skip to content

Instantly share code, notes, and snippets.

@kkirsanov
Created December 19, 2011 14:54
Show Gist options
  • Save kkirsanov/1497547 to your computer and use it in GitHub Desktop.
Save kkirsanov/1497547 to your computer and use it in GitHub Desktop.
RLE
import Data.List (group)
rle:: String -> [(Char, Int)]
rle [] = error "Empty List"
rle [x] = [(x, 1)]
rle st@(x:xs) = (head start, cnt) : rle ending where
start = takeWhile (==x) st
cnt = length start
ending = drop cnt st
-- with library
rle2:: String -> [(Char, Int)]
rle2 = map (\g -> (head g, length g)) . group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment