Run-length encoding

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression.

The output can be anything, as long as you can recreate the input with it.



Example

 Input: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
 Output: 12W1B12W3B24W1B14W


Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.




declare
  fun {RLEncode Xs}
     for G in {Group Xs} collect:C do
	{C {Length G}#G.1}
     end
  end
 
  fun {RLDecode Xs}
     for C#Y in Xs append:Ap do
	{Ap {Replicate Y C}}
     end
  end
 
  %% Helpers
  %% e.g. "1122" -> ["11" "22"]
  fun {Group Xs}
     case Xs of nil then nil
     [] X|Xr then
	Ys Zs
        {List.takeDropWhile Xr fun {$ W} W==X end ?Ys ?Zs}
     in
        (X|Ys) | {Group Zs}
     end
  end
  %% e.g. 3,4 -> [3 3 3 3] 
  fun {Replicate X N}
     case N of 0 then nil
     else X|{Replicate X N-1}
     end
  end
 
  Data = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
  Enc = {RLEncode Data}
in
  {System.showInfo Data}
  {Show Enc}
  {System.showInfo {RLDecode Enc}}