Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Run-length-encoding/Haskell/run-length-encoding-3.hs
Normal file
25
Task/Run-length-encoding/Haskell/run-length-encoding-3.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import Data.Char (isDigit)
|
||||
import Data.List (span)
|
||||
|
||||
encode :: String -> String
|
||||
encode [] = []
|
||||
encode (x : xs) =
|
||||
let (run, rest) = span (x ==) xs
|
||||
in x : (show . succ . length) run <> encode rest
|
||||
|
||||
decode :: String -> String
|
||||
decode [] = []
|
||||
decode (x : xs) =
|
||||
let (ds, rest) = span isDigit xs
|
||||
n = read ds :: Int
|
||||
in replicate n x <> decode rest
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn encoded
|
||||
>> putStrLn decoded
|
||||
>> print (src == decoded)
|
||||
where
|
||||
src = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
encoded = encode src
|
||||
decoded = decode encoded
|
||||
Loading…
Add table
Add a link
Reference in a new issue