Skip to content

Instantly share code, notes, and snippets.

@mainyaa
Created November 29, 2012 11:30
Show Gist options
  • Save mainyaa/4168377 to your computer and use it in GitHub Desktop.
Save mainyaa/4168377 to your computer and use it in GitHub Desktop.
run length encoding on haskell
import Data.List (group)
import Control.Arrow
main = putStrLn answer
solve = concatMap (uncurry (:) . (head &&& show.length)) . group
answer = solve "aaaabb"
module RunLengthEncoding where
import Data.List (group)
import Control.Arrow ((&&&))
main :: IO ()
main = putStrLn (runLengthEncoding "aaaabb")
-- runLengthEncoding
-- runLengthEncodingは、Stringを受け取り、Run-Length EncodingしたStringを返却する
-- @param: String
-- @returns: String
runLengthEncoding :: String -> String
runLengthEncoding = concatMap encodingMap . group
encodingMap :: [Char] -> [Char]
encodingMap = (uncurry addList . encoding)
-- addList
-- addListは、要素とListを受け取り、結合したListを返却する
-- @param: a
-- @param: [a]
-- @returns: [a]
addList :: a -> [a] -> [a]
addList = (:)
encoding :: [c] -> (c, String)
encoding = (head &&& show.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment