Run-length encoding: Difference between revisions
→Haskell :: As a fold: Slight reduction – pruned out an import.
(→Haskell: Added a variant expressed as a fold.) |
(→Haskell :: As a fold: Slight reduction – pruned out an import.) |
||
Line 2,911:
===As a fold===
<lang haskell>----------------------- RUN LENGTHS ----------------------▼
▲----------------------- RUN LENGTHS ----------------------
runLengths :: String -> [(Int, Char)]
runLengths "" = []
runLengths s = uncurry (:) (foldr go ((0, ' '), []) s)
where
| c
| otherwise = ((1, c), (n, x) : xs)
--------------------------- TEST -------------------------
Line 2,933 ⟶ 2,926:
main = do
let testString =
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWW"
<> "
encoded = runLengths testString
putStrLn $ showLengths encoded
Line 2,941 ⟶ 2,934:
------------------------- DISPLAY ------------------------
showLengths :: [(Int, Char)] -> String
showLengths [] = []</lang>
{{Out}}
<pre>12W1B12W3B24W1B14W
|