Problem #10

"Run-length encoding of a list."

And my solution: 

    1 #light

    2 

    3 let oSnap = [1;1;1;2;2;2;2;2;2;3;3;3;3;3;3;4;]

    4 

    5 let prob10 listupendous =

    6     let rec packer listy tempy head tail =   

    7         match tail with

    8         | h::t -> if head = h then

    9                     packer listy (tempy @ [head]) h t

   10                   else

   11                     packer (listy @ [(List.length (tempy @ [head]), head)]) [] h t

   12         | []        -> listy @ [1 , head]

   13 

   14     match listupendous with

   15     | h::t   -> packer [] [] h t

   16     | []    -> [1, (listupendous |> List.hd)]

  Like last time, it's a lot like my implementation of problem 8 and 9 with one difference. Instead of returning a list or a list of a list, we're returning a tuple with the count of how many of each element AND the value of the element. 

I'd like to thank Claudio Cherubino for his post on run length encoding which gave some great insight on the problem. 

 


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories:

0 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 28. 2008 14:15