% ex.pl % some example predicates that might help with assignment # 11 % uniquify a list--keep the last occurence of each element uniq([], []). uniq([X | R], R2) :- member(X, R), uniq(R, R2). uniq([X | R], [X | R2]) :- \+member(X, R), uniq(R, R2). % remove an element (which much be present) from a list... removeOne(X,[X|R],R). removeOne(X,[F|R],[F|S]) :- removeOne(X,R,S). removeAll(X,[X|R],R) :- \+member(X,R). removeAll(X,[F|R],[F|S]) :- removeAll(X,R,S), X\==F. removeAll(X,[X|R],R2) :- removeAll(X, R, R2). % generates permutations of the first input's list perm([X|Y],Z) :- perm(Y,W), remove(X,Z,W). perm([],[]). % checks if the input list is sorted sorted( [] ). sorted( [_] ). sorted( [F,S|R] ) :- sorted( [S|R] ), F < S. % binds the second argument to a sorted version of the first mysort( [], [] ). mysort( L, S ) :- perm( L, S ), sorted(S). % splits the first argument's list into subsets, i.e., % sublists in which the order of elements may vary split( List, A, B ) :- perm( List, L2 ), append( A, B, L2 ).