3 ms·
I see your points - I'll certainly use clpfd more, in future. Also, I was mistaken - my peano-ish solution worked in both directions, though it had probable pe
by ghusbands 3y ago
I see your points - I'll certainly use clpfd more, in future.
Also, I was mistaken - my peano-ish solution worked in both directions, though it had probable performance issues (perhaps because I didn't know `dif/2` was more flexible than `\=`) and then had trouble when I tried to map back from peano to naturals. I used a fun and perhaps misleading form to try for brevity:
rle([], []).
rle([X], [[1, X]]).
rle([X,X|XS], [[N+1,X]|ZS]) :- rle([X|XS], [[N,X]|ZS]).
rle([X,Y|XS], [[1,X]|ZS]) :- rle([Y|XS], ZS), X\=Y.
And so:
?- rle([7,7,7,8,8,9], Y).
Y = [[1+1+1, 7], [1+1, 8], [1, 9]];
false.
?- rle(X, [[1+1+1, 7], [1+1, 8], [1, 9]]).
X = [7, 7, 7, 8, 8, 9];
false.
Edit: If one cared for safety/compatibility, each [1, X] would be [0+1, X].