count([], [], 0). count([_|IT], IT, 1). count([IH|IT], OL, N1) :- IT=[IH|_], count(IT,OL,N), N1 is N+1. headisnot([],_). headisnot([[_,H]|_],E) :- H \= E. rle([], []). rle([IH|IT], [[N,IH]|OT]) :- count([IH|IT],NI,N), rle(NI,OT), headisnot(OT, IH).