Run-Length Encoding in Haskell


/ Published in: Haskell
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

URL: http://www.haskell.org/haskellwiki/99_questions/1_to_10

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.