RosettaCodeData/Task/Run-length-encoding/Picat/run-length-encoding-3.picat

12 lines
318 B
Plaintext

rle3(S) = RLE =>
rle3(S.tail(),S[1],1,[],RLE).
rle3([],LastChar,Count,RLE1,RLE) =>
RLE = (RLE1 ++ [Count.to_string(),LastChar.to_string()]).join('').
rle3([C|T],LastChar,Count,RLE1,RLE) =>
C == LastChar ->
rle3(T,C,Count+1,RLE1,RLE)
;
rle3(T,C,1,RLE1++[Count.to_string()++LastChar.to_string()],RLE).