如何正确过滤返回多个重复值的子句? [英] How to correctly filter a clause that returns multiple duplicated values?

查看:55
本文介绍了如何正确过滤返回多个重复值的子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试正确过滤从子句返回的值(它返回多个重复值).我很难理解逻辑编程,抱歉,如果这是一个愚蠢的问题.

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.

每个城市最贵的:

home(peter, sanFrancisco, 1000).
home(juan, california, 700).
home(peter, vegas, 100).

每个城市第二贵:

home(juan, sanFrancisco, 400).
home(ash, california, 600).
home(ash, vegas, 80).

我期望的结果:

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).

我认为 superExpensive 的某个地方正在做一些像每个人一样的事情*每个人?

I think somewhere in superExpensive is doing something like everyone * everyone?

推荐答案

镇上最贵的房子是镇上没有其他房子比它更贵的:

The most expensive home in a town is such that no other home in that town is more expensive than it:

most_expensive( home( Name, Town, Price)):-
  home( Name, Town, Price),
  \+ (home( _, Town, P), P > Price).

这让我们

5 ?- most_expensive( H ).
H = home(peter, sanFrancisco, 1000) ;
H = home(juan, california, 700) ;
H = home(peter, vegas, 100) ;
false.

镇中第二贵的房屋是该镇中不是最贵的房屋中最贵的:

The second most expensive home in a town is such that is the most expensive among the homes which are not the most expensive home in that town:

second_most_expensive( home( Name, Town, Price)):-
  most_expensive( home( _, Town, TopPrice) ),
  home( Name, Town, Price), Price < TopPrice,
  \+ (home( _, Town, P), P < TopPrice, P > Price).

这让我们受益

8 ?- second_most_expensive( H ).
H = home(juan, sanFrancisco, 400) ;
H = home(ash, california, 600) ;
H = home(ash, vegas, 80) ;
false.

那么,与

top_house_owner( Name ) :-
  most_expensive( home( Name, T, P) ),
  second_most_expensive( home( _, T, P2 ) ),
  P2 < P div 2.

我们得到

12 ?- top_house_owner( N ).
N = peter ;
false.

这篇关于如何正确过滤返回多个重复值的子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆