src/RunLength.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module RunLength (decode, encode) where

import Control.Arrow ((&&&))
import Data.Char     (isDigit)
import Data.List     (group)

encode :: String -> String
encode =
  concatMap (uncurry (++)) . fmap (show' . length &&& (:[]) . head) . group
  where show' n = if n == 1 then "" else show n

decode :: String -> String
decode [] = []
decode s  = replicate (read' i) (head s') ++ decode (tail s')
 where
  (i, s') = span isDigit s
  read' i = if i == "" then 1 else read i

@kirsty-tortoise thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment