[ create a new paste ] login | about

Link: http://codepad.org/VakQUriW    [ raw code | output | fork ]

Haskell, pasted on Feb 26:
import Control.Arrow
import Data.List
import Test.QuickCheck
import Data.Char

instance Arbitrary Char where
    arbitrary     = choose ('\0', '\255')
    coarbitrary c = variant (ord c `rem` 4)

compress :: String -> String
compress xs = group xs >>= (head &&& length >>> encode)
    where encode (c, n) | c == '~'  = concat . replicate n $ "~A~"
                        | n < 4     = replicate n c
                        | n > 26    = encode (c, 26) ++ encode (c, n-26)
                        | otherwise = '~' : toEnum (fromEnum 'A' + n - 1 ) : [c]

expand :: String -> String
expand [] = []
expand ('~':n:c:cs) = replicate (fromEnum n - fromEnum 'A' + 1) c ++ expand cs
expand (c:cs) = c : expand cs


main = quickCheck prop_expandcompress >>= print

prop_expandcompress x = (expand . compress) x == id x
    where _ = x :: String


Output:
1
2
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OK, passed 100 tests.
()


Create a new paste based on this one


Comments: