%append-list(L1,L2,L) = L = L1 + L2
append-list([],R,R).
append-list([H|T],U,[H|S]):-append-list(T,U,S).

%reverse-list(List, Reversed)
reverse-list([],[]).
reverse-list([H|T], R):-reverse-list(T,Reversed),
						append-list(Reversed,[H],R).
rev(L,R):-  accRev(L,[],R).
accRev([H|T],A,R):-  accRev(T,[H|A],R). 
accRev([],A,A).

length-list([],0).
length-list([_|T],R):-length-list(T, R2), R is R2 + 1.

qp(A, B, C):-rev(A, S), append-list(B, S, C).
qp(A, B, C):-rev(A, S), append-list(B, S, D), 
		length-list(D, N), length-list(C, M), 
		N < M, qp(B, D, C).

% heads(H, M) H e purvata kolona na M
heads([], []).
heads([H|Heads1], [[H|_]|T]):-heads(Heads1, T).

tails([], []).
tails([T|Tails1], [[_|T]|R]):-tails(Tails1, R).

%transpose(T, M). T e transponiratnata M
transpose([], M):-flatten(M, []).
transpose([H|T], M):-heads(H, M), tails(T1, M), transpose(T, T1).

is-member(E, [E|_]).
is-member(E, [_|T]):-is-member(E, T).

% append2(L1, L2, L):- L e concat. na L1 i L2

append2([], L, L).
append2([H|T], L2, [H|S]):-append2(T, L2, S).

set([],[]).
set([H|T],[H|S]) :- not(is-member(H,T)), set(T,S).
set([H|T],S) :- is-member(H,T), set(T,S).

delMember(_, [], []).
delMember(X, [X|Xs], Y) :- delMember(X, Xs, Y).
delMember(X, [T|Xs], [T|Y]):- delMember(X, Xs, Y).

%% gensubset(L, M) :- Generates all subsets of M in L 
gensubset([], []).
gensubset([H|T], [H|S]) :- gensubset(T, S).
gensubset(T, [_|S]) :- gensubset(T, S).


