Posted By

rtperson on 07/29/11


Tagged

algorithm problems 99


Versions (?)

Run-Length Encoding in Haskell


 / Published in: Haskell
 

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

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

Report this snippet  

You need to login to post a comment.