Run-length encoding: Difference between revisions
→Haskell In terms of group: Tidied. Pruned out one import, added output.
(→{{header|Haskell}}: Added a variant expressed in terms of span.) |
(→Haskell In terms of group: Tidied. Pruned out one import, added output.) |
||
Line 2,431:
===In terms of group===
<lang haskell>import Data.List (group)
-- Datatypes
type Encoded = [(Int, Char)]
type Decoded = String
-- Takes a decoded string and returns an encoded list of tuples
rlencode :: Decoded -> Encoded
rlencode =
-- Takes an encoded list of tuples and returns the associated decoded String
Line 2,447:
main :: IO ()
main = do
let input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
-- Output encoded and decoded versions of input▼
▲ -- Output encoded and decoded versions of input
▲ let encoded = rlencode input
decoded = rldecode encoded
putStrLn $ "Encoded: "
{{Out}}
<pre>Encoded: [(12,'W'),(1,'B'),(12,'W'),(3,'B'),(24,'W'),(1,'B'),(14,'W')]
Decoded: "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</pre>
Or:
|