I'm trying to correctly filter the values returned from a clause (it is returning multiple duplicated values). I'm having a hard time understanding the logic programming, sorry if its an stupid question.
These are my facts/predicates:
home(peter, sanFrancisco, 1000).
home(ash, sanFrancisco, 100).
home(juan, sanFrancisco, 400).
home(juan, california, 700).
home(ash, california, 600).
home(peter, california, 500).
home(peter, vegas, 100).
home(ash, vegas, 80).
home(juan, vegas, 60).
What im trying to do is to retrieve the name; the condition is that I have to retrieve only the ones that from a certain city their home is the most expensive from there but also if the second most expensive home from that same city is less than half the price of the first. I cannot use lists.
The most expensive from each city:
home(peter, sanFrancisco, 1000).
home(juan, california, 700).
home(peter, vegas, 100).
Second most expensive from each city:
home(juan, sanFrancisco, 400).
home(ash, california, 600).
home(ash, vegas, 80).
What I expect as a result:
peter.
What I have tried so far but with no success..
%Return the most expensive from each city.
theMostExpensive(Name, City):-
home(Name, City, Price),
fromEach(City, Price).
fromEach(City, Price):-
forall(home(_, City, Price2), Price>= Price2).
%Return the second most expensive from each city. Not sure if working correctly.
secondMostExpensive(Name, City):-
owner(home),
not(theMostExpensive(Name, City)),
theMostExpensive(Name2, City),
Name \= Name2.
%Return a lot of duplicated values and wrong..
superExpensive(Name):-
theMostExpensive(Name, City),
secondMostExpensive(Name2, City),
Name \= Name2,
City \= City2,
home(Name, City, Price),
home(Name2, City2, Price2),
Price > Price2 + (Price / 2).
I think somewhere in superExpensive is doing something like everyone * everyone?