%a

%nrOccurences(L:list, E:number, R:number)
%flow model: (i,i,o)
%nrOccurences(l1l2..ln, e) = 0, if n = 0
%                           1 + nrOccurences(l2..ln, e), if l1 = e
%                           nrOccurences(l2..ln, e), if l1 != e
nrOccurences([],_,0).
nrOccurences([H|T],E,R):-
    H=:=E,
    nrOccurences(T,E,R1),
    R is R1+1.
nrOccurences([H|T],E,R):-
    H=\=E,
    nrOccurences(T,E,R).

%removeElem(L:list, E:number, R:list)
%flow model: (i,i,o)
%removeElem(l1l2..ln, e) = [], if n = 0
%                          removeElem(l2..ln, e), if l1 = e
%                          l1 + removeElem(l2..ln, e), if l1 != e
removeElem([],_,[]).
removeElem([H|T],E,R):-
    H=:=E,
    removeElem(T,E,R).
removeElem([H|T],E,R):-
    H=\=E,
    removeElem(T,E,R1),
    R=[H|R1].

%remove(L:list, R:list)
%flow model: (i,o)
%remove(l1l2..ln) = removeRepetitive(l1l2..ln, l1l2..ln)
remove(L,R):-
    removeRepetitive(L,L,R).

%removeRepetitive(L:list, D:list, R:list)
%flow model: (i,i,o)
%removeRepetitive(l1l2..ln, d1d2..dm) = [], if n = 0
%                                       l1 + removeRepetitive(l2..ln, d1d2..dm), if nrOccurences(d1d2..dm, l1) = 1
%                                       removeRepetitive(l2..ln, d1d2..dm), if nrOccurences(d1d2..dm, l1) != 1
removeRepetitive([],_,[]).
removeRepetitive([H|T],D,R):-
    nrOccurences(D,H,R1),
    R1=:=1,
    removeRepetitive(T,D,R2),
    R=[H|R2].
removeRepetitive([H|T],D,R):-
    nrOccurences(D,H,R1),
    R1=\=1,
    removeRepetitive(T,D,R).



%b

%max(L:list, E:number)      
%flow model: (i,o)
%max(l1l2..ln) = l1, if n = 1
%                max(l2..ln), if l1 < max(l2..ln)
%                l1, if l1 >= max(l2..ln)
max([E],E).
max([H|T],E):-
    max(T,E1),
    H>E1,
    E is H.
max([H|T],E):-
    max(T,E1),
    H=<E1,
    E is E1.

%removeMax(L:list, R:list)
%flow model: (i,o)
%removeMax(l1l2..ln) = [], if n = 0
%                      removeElem(l1l2..ln, max(l1l2..ln)), if n > 0
removeMax([],[]).
removeMax(L,R):-
    max(L,E),
    removeElem(L,E,R).


