Skip to content

Instantly share code, notes, and snippets.

@mwilliamson mwilliamson/gist:7378638
Last active Dec 27, 2015

Embed
What would you like to do?
Encode and de-code run-length encoding in Prolog with a single predicate.
encode([], []).
encode(XS, [E|ES]) :-
reverse(XS, XSReversed),
append(XRightReversed, XLeft, XSReversed),
XLeft \= [],
expand(XLeft, E),
reverse(XRightReversed, XRight),
encode(XRight, ES).
expand([X|XT], [Count, X]) :- expand(XT, [SubCount, X]), succ(SubCount, Count).
expand([], [0, _]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.