prolog find minimum fact with two conditions

I'm working on educational project about Prolog, but I haven't found a solution on this problem.

I have a list of facts like these:

car(1,1,1).
car(2,2,2).
car(3,1,2).
car(4,2,3).

What I have to do is to take the fact where the second parameter is maximum, and, if exists two facts with the second parameter equal, take the fact where the second parameter is maximum and the third parameter is minimum. For example, in the list of facts, I would be to select car(2,2,2) because it has the maximum value of the second parameter, and the minimum of the third.

I have tried many times, but I don't know how to proceed.

Anyone could help me?

Upvotes: 1

Views: 161

Answers (1)

rajashekar
rajashekar

Reputation: 3793

A straight forward solution. Find the max second parameter and min third parameter with second as required.

req(car(A, B, C)) :-
    findall(X, car(_, X, _), Xs),
    max_list(Xs, B),
    findall(Y, car(_, B, Y), Ys),
    min_list(Ys, C),
    car(A, B, C).
?- req(Car).
Car = car(2, 2, 2) 

Upvotes: 1

Related Questions