Prolog数据库查询

发布于 2024-12-15 04:36:22 字数 896 浏览 4 评论 0原文

创建一个包含以下形式事实的约会数据库:

person(姓名、年龄、性别、身高、体重、教育程度、薪水)

给定输入:

datable(alex, X)。

datable_weight_limit(alex, 170, X). 其中170是体重的阈值

如何根据规则返回潜在匹配的姓名:

  1. 只能匹配异性
  2. 男性应该年龄较大比女性
  3. 男性应该比女性接受更高的教育吗?

我已经建立了这些关系:

edu_less(hs, bachelor).
edu_less(bachelor, master).
edu_less(master, phd).

edu_lesser(A, B):-
    edu_less(A, X),
    edu_lesser(X, B).

我已经尝试过:

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    person(Namem, Agem, Sexm, Heightm, Weightm, Educationm, Salarym),

    Sexm \== Sexl.

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    Sexl == female,

    findall( X, person(X, _, male, _, _, _, _), Y).

但我似乎没有运气。是否有我遗漏的功能或我实施的错误?

To make a dating database with facts in the form of:

person(name, age, sex, height, weight, education, salary)

Given the input of:

datable(alex, X).

datable_weight_limit(alex, 170, X). Where 170 is the threshold for weight

How can I return the names of potential matches with the rules:

  1. Only opposite sexes can be matched
  2. Males should be older than females
  3. Males should have a higher education than females?

I have these relations set up:

edu_less(hs, bachelor).
edu_less(bachelor, master).
edu_less(master, phd).

edu_lesser(A, B):-
    edu_less(A, X),
    edu_lesser(X, B).

I've tried:

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    person(Namem, Agem, Sexm, Heightm, Weightm, Educationm, Salarym),

    Sexm \== Sexl.

datable(X, Y):-

    person(X, Agel, Sexl, Heightl, Weightl, Educationl, Salaryl),

    Sexl == female,

    findall( X, person(X, _, male, _, _, _, _), Y).

But I seem to have no luck. Is there a function I'm missing or something I'm implementing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-12-22 04:36:22

我认为你几乎是正确的。试试这个:

datable(M, F):-
    person(M, AgeM, male, HeightM, WeightM, EducationM, SalaryM),
    person(F, AgeF, female, HeightF, WeightF, EducationF, SalaryF),
    AgeM >= AgeF,
    edu_lesser(EducationF, EducationM),
    datable_weight_limit(M, MaxWeight),
    MaxWeight >= WeightF. 

在这里我们假设教育也是数字。
如果您还需要函数 - datable(F,M)

datable(F,M) :-
     person(F, _, female, _, _, _, _),
     datable(M, F).

I think you are almost correct. Try this:

datable(M, F):-
    person(M, AgeM, male, HeightM, WeightM, EducationM, SalaryM),
    person(F, AgeF, female, HeightF, WeightF, EducationF, SalaryF),
    AgeM >= AgeF,
    edu_lesser(EducationF, EducationM),
    datable_weight_limit(M, MaxWeight),
    MaxWeight >= WeightF. 

Here we suppose, that education is also numbers.
If you need also function - datable(F,M):

datable(F,M) :-
     person(F, _, female, _, _, _, _),
     datable(M, F).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文