Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
%Compresses each sequence of consecutive identical ground terms.
%The first argument may contain consecutive identical terms.
%The second argument contains no consecutive identical terms.
compress([],[]) :- !.
compress([X, X|Xs], [X|Ys]) :- !, compress([X|Xs], [X|Ys]).
compress([X|Xs], [X|Ys]) :- compress(Xs, Ys).
%Flattens a list of lists into a flat list.
%The first argument should be the unflattened list.
%The second argument should be the flattened list.
my_flatten([],[]) :- !.
my_flatten([X|Xs], Ys) :- !, my_flatten(X, Y1), my_flatten(Xs, Y2), append(Y1, Y2, Ys).
my_flatten(X, [X]).
% Creates a list of lists with consecutive repetitions of ground terms.
% The first argument may contain consecutive repetitions.
% The second argument groups the consecutive repetitions into lists and
% returns a list of these lists.
pack([],[]) :- !.
pack([X],[[X]]) :- !.
pack([X, Y|Xs],[[X]|Ys]) :- X\=Y, !, pack([Y|Xs],Ys).
pack([X|Xs],[[X|Y]|Ys]) :- pack(Xs,[Y|Ys]).
%Creates a list of lists counting the number of consecutive repetitions.
%The first argument should be a list with consecutive repetitions.
%The second argument is the list with the count of repetitions.
rlencode([],[]) :- !.
rlencode([X],[Y]) :- pack(X,[[H|Hs]|T]), length([H|Hs],Y1), rlencode(T,Y2),append([H|Y1],Y2,Y).
%Creates the original list from the list of lists produced by rlencode.
%The first argument is a list with the count of repetitions.
%The second argument will be the list with consecutive repetitions.
rldecode([],[]) :- !.
rldecode([[X|1]|Xs],[X|Ys]) :- !, rldecode(Xs, Ys).
rldecode([[X|N]|Xs],[X|Ys]) :- N1 is N-1, rldecode([[X|N1]|Xs],[Ys]), !.
rldecode([X|Xs],[X|Ys]) :- rldecode(Xs, Ys).
%Creates a list of consecutive integers between two bounds.
%The first argument is the lower bound.
%The second argument is the upper bound.
%The third argument is the list of integers.
range(X,X,[X]) :- integer(X), !.
range(X,Y,Z) :- integer(X), integer(Y), !, X1 is X+1, range(X1,Y,W), append([X],W,Z).