(Haskell) Run-Length Encoding in Haskell

Problem 10 of the famous 99 Problems. I got 99 problems, but a Lisp ain’t one.

  1. import Data.List
  2.  
  3. thing :: String
  4. thing = "aaaabccaadeeee"
  5.  
  6. – My attempt got me close enough to consider it solved.
  7. encode :: (Eq a) => [a] -> [(Int, [a])]
  8. encode xs =
  9.     let groupList = groupBy (\x y -> x == y) xs
  10.         lengths = map length groupList
  11.         letterList = map nub groupList
  12.     in zip lengths letterList
  13.    
  14. – from the haskell.org solutions:
  15. encode2 :: (Eq a) => [a] -> [(Int, a)]
  16. encode2 = map (\x -> (length x, head x)) . group
This entry was posted in Haskell and tagged , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?

  • Advertisement

  • Categories