I am trying to encrypt the string "aaaabbccdeee" gets converted into "a4bbccde3"
I wasn't able to get it working in one function
Working Solution 1:
import Data.List (group)
gab0 :: [Char] -> [(Char, Int)]
gab0 = map(\l@(x:xs) -> (x, length l)) . group
gab1 :: (Char, Int) -> String
gab1 (d, 1) = [d]
gab1 (d, 2) = [d] ++ [d]
gab1 (d, x) = [d] ++ show x
gab :: [(Char, Int)] -> String
gab = foldr(\x acc -> gab1 x ++ acc) []
{-
- gab $ gab0 ['a', 'a', 'a', 'b', 'c', 'c','c','c','d','e','e']
"a3bc4de2"
-}
Solution 2 not working:
gab2 :: [Char] -> Int -> [Char]
gab2 [x] n
| n > 1 = [x] ++ show n
| otherwise = [x]
gab2 (x:y:xs) n
| x == y = gab2 (x:xs) (n + 1)
| otherwise = getnum x : gab2 (y:xs) 0
where getnum a = [a] ++ show n ++ []
"aaaabbccdeee" transformed to "a4bbccde3"
"34"to"3141", rather than"34", if you want to handle all possible inputs.