My code is supposed to output a schedule for a competetion the predicate scheduleround2 is used to generate a list of game(player1,player2,round).
however there is an error most probably with the unification for Z.
here is the code:
sample input:
schedule_round([player(alex,2), player(jane,5), player(djokovich,1), player(nadal,3), player(anderson,4), player(jack,6), player(nilson,7), player(pete,8)], R).
and the output should be
R=[game(pete,djokovich,quarter_final), game(nilson,alex,quarter_final), game(jack,nadal,quarter_final), game(jack,jane,quarter_final)]
What actually happens is that is computes forever producing nothing
%gets the maximum player rating
maxf([H|T], R) :- maximum(T, H, R).
maximum([], R, R).
maximum([player(X,Y)|T], player(_,R), F) :-
Y > R,
maximum(T, player(X,Y), F).
maximum([player(_,Y)|T], player(Z,R), F) :-
R > Y,
maximum(T, player(Z,R), F).
%gets the minimum player rating
minf([H|T], R) :- minimum(T, H, R).
minimum([], R, R).
minimum([player(X,Y)|T], player(_,R), F):-
Y < R,
minimum(T, player(X,Y), F).
minimum([player(_,Y)|T], player(Z,R), F):-
R < Y,
minimum(T, player(Z,R), F).
%removes players who are scheduled from the list
del(X, [X|L], L).
del(X, [A|L], [A|L1]) :- del(X, L, L1).
%gets size of the list
size([], 0).
size([_|T], N) :- size(T, M), N is M+1.
%scheduling predicate
schedule_round(X, R) :-
size(X, N),
schedule_round2(N, X, R).
%helper
schedule_round2(0, _, []).
schedule_round2(N, H, Z) :-
N2 is N-2,
maxf(H, F),
minf(H, B),
del(F, H, L),
del(B, L, J),
append(Z, game(F, B, quarter_final), K),
schedule_round2(N2, J, K).
sizepredicate. ISO prolog haslength/2which gives you the length of a list:length(List, N). - lurkerdel(X, [A|L], [A|L1]) :-...needs to check thatX \== A. Otherwise prolog will backtrack to and succeed on that clause even ifXandAare the same. And I assume you are querying with a list of an even number of elements, but just in case,schedule_round2(N, H, Z) :-...should first check,N > 1. - lurker