Created
November 29, 2012 11:30
-
-
Save mainyaa/4168377 to your computer and use it in GitHub Desktop.
run length encoding on haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (group) | |
import Control.Arrow | |
main = putStrLn answer | |
solve = concatMap (uncurry (:) . (head &&& show.length)) . group | |
answer = solve "aaaabb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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