Skip to content

Instantly share code, notes, and snippets.

@erikrozendaal
Created December 9, 2013 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikrozendaal/7877107 to your computer and use it in GitHub Desktop.
Save erikrozendaal/7877107 to your computer and use it in GitHub Desktop.
Run-length encoding in Idris
rep : (n : Nat) -> a -> List a
rep Z x = []
rep (S k) x = x :: rep k x
data RLE : List Char -> Type where
REnd : RLE []
RChar : (n : Nat) -> (c : Char) -> (rs : RLE xs) -> RLE (rep n c ++ xs)
rle : (xs : List Char) -> RLE xs
rle [] = REnd
rle (x :: xs) with (rle xs)
rle (x :: []) | REnd = RChar 1 x REnd
rle (x :: (rep n c ++ ys)) | (RChar n c rs) with (decEq x c)
rle (x :: (rep n x ++ ys)) | (RChar n x rs) | (Yes refl) = RChar (S n) x rs
rle (x :: (rep n c ++ ys)) | (RChar n c rs) | (No f) = RChar 1 x (RChar n c rs)
data Singleton : (v : a) -> Type where
MkSingleton : (v : a) -> Singleton v
unrle : RLE xs -> Singleton xs
unrle REnd = MkSingleton []
unrle (RChar n c rs) with (unrle rs)
unrle (RChar n c rs) | (MkSingleton xs) = MkSingleton (rep n c ++ xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment