Idris-dev/test/basic001/basic001a.idr
Edwin Brady 43127b17a7 Move Fin, Vect and So from prelude to base
They can be imported from the modules Data.Fin, Data.Vect, and Data.So
respectively.

The general thinking here is that not every program is going to need
these, and they are often used especially by newcomers in place of
something more appropriate. Also, all of them are useful for teaching,
which means it is instructive for tutorials to introduce them and have
people implement them themselves.
2014-12-31 20:18:02 +00:00

35 lines
1017 B
Idris

module Main
import Data.Vect
data RLE : Vect n Char -> Type where
REnd : RLE []
RChar : {xs : Vect k Char} ->
(n : Nat) -> (c : Char) -> (rs : RLE xs) ->
RLE (replicate (S n) c ++ xs)
------------
rle : (xs : Vect n Char) -> RLE xs
rle [] = REnd
rle (x :: xs) with (rle xs)
rle (x :: []) | REnd = RChar 0 x REnd
rle (x :: (c :: (replicate n c ++ ys))) | (RChar n c rs) with (decEq x c)
rle (x :: (x :: (replicate n x ++ ys))) | (RChar n x rs) | (Yes Refl)
= RChar (S n) x rs
rle (x :: (c :: (replicate n c ++ ys))) | (RChar n c rs) | (No f)
= RChar 0 x (RChar n c rs)
compress : Vect n Char -> String
compress xs with (rle xs)
compress [] | REnd = ""
compress (c :: (replicate n c ++ xs1)) | (RChar n c rs)
= show (the Integer (cast (S n))) ++
strCons c (compress xs1)
compressString : String -> String
compressString xs = compress (fromList (unpack xs))
main : IO ()
main = putStrLn (compressString "foooobaaaarbaaaz")