My task was changed a little. And I need to implement abs and max also in prolog. I need to take list of integer and tell if abs value of max is greater then abs value of min. – M
My code is like this :
ismaxgreater([X],X).
ismaxgreater([X],X):-
maxlist([X],X).
%minlist([X],Y),
%abs([Y],Z),
%com(X,Z).
maxlist([X],X).
maxlist([X,Y |Rest],Max) :-
maxlist([Y | Rest], MaxRest),
max(X,MaxRest,Max).
minlist([X],X).
minlist([X,Y |Rest],Min) :-
maxlist([Y | Rest], MinRest),
max(X,MinRest,Min).
com(Max,Min) :- Max>Min,
write('Max is Bigger Value');
Max<Min,
write('Min is Bigger Value').
max(X,Y,X) :- X>= Y.
max(X,Y,Y) :- X < Y.
min(X,Y,X) :- X =< Y.
min(X,Y,Y) :- X > Y.
abs(X,Y) :- X < 0 -> Y is -X; Y = X.
But I want to enter on Input only like this :
ismaxgreater([1,2,-5,9])
where output should be like :
Max is greater true
In this solution, we find the max and min values and compare Max with the absolute value of Min.
max([X], X) :- !, true.
max([X|List], M):- max(List, M), M >= X.
max([X|List], X):- max(List, M), X > M.
min([X], X) :- !, true.
min([X|List], M):- min(List, M), M <= X.
min([X|List], X):- min(List, M), X > M.
ismaxgreater(X) :- max(X, Max), min(X, Min), Max > abs(Min);