Hi, I'm new to prolog. I have folowing predicated in my program:
points(bob, 3).
points(ted, 10).
points(tom, 7).
points(ben, 5).
How do I find out who has the most points?
I don't know how to make Prolog iterate through all points() predicates and remember the highest value. Iterating through a list would be easier, but I'd like to stay away from lists if possible.
My first attempt was:
more_points(X,Y):- points(X, P1), points(Y, P2), P1 > P2.
less_points(X,Y):- points(X, P1), points(Y, P2), P1 < P2.
same_points(X,Y):- points(X, P1), points(Y, P2), P1 == P2, X \= Y.
most_points(X):- less_points(_, X),\+ more_points(_, X), \+ same_points(X, Y), !.
But I think it isn't the best way to do it and I cant come up with a better one. Does anyone know a better way?
points(bob, 3).
points(ted, 10).
points(tom, 7).
points(ben, 5).
How do I find out who has the most points?
I don't know how to make Prolog iterate through all points() predicates and remember the highest value. Iterating through a list would be easier, but I'd like to stay away from lists if possible.
My first attempt was:
more_points(X,Y):- points(X, P1), points(Y, P2), P1 > P2.
less_points(X,Y):- points(X, P1), points(Y, P2), P1 < P2.
same_points(X,Y):- points(X, P1), points(Y, P2), P1 == P2, X \= Y.
most_points(X):- less_points(_, X),\+ more_points(_, X), \+ same_points(X, Y), !.
But I think it isn't the best way to do it and I cant come up with a better one. Does anyone know a better way?