notbeginswith(_, []). notbeginswith(IE,[OE|_]) :- IE \= OE. rle([], []). rle([[1,OE]|IT], [OE|OT]) :- rle(IT, OT), notbeginswith(OE, OT). rle([[N1,E]|IT], [E|OT]) :- notbeginswith(E, IT), rle([[N,E]|IT], OT), N1 is N+1. % rle([[2,a]],[a,a]). % rle([[1,a],[2,b],[3,c]],[a,b,b,c,c,c]). % rle([[1,a],[2,b],[3,c]],What). % rle(What,[a,b,b,c,c,c]).