本文介绍了Prolog:获取一个或多个不符合事实的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下事实:

boy(a).
boy(b).
girl(c).
girl(d).

如果我查询:

?-boy(X).

我明白了:

X=a;
X=b.

我应该使用哪个查询使用变量来获取不符合规则 boy() 的对象,在本例中为 c 和 d?

Which query should I use using a variable to get the objects that do not comply with the rule boy(), in this case, c and d?

我是 Prolog 的新手,所以我在考虑使用

I am new to Prolog, so I was thinking about using

?-not(boy(X)).

但这并不正确.我正在使用 swi-prolog.提前感谢您抽出宝贵时间和帮助.

But that is not correct. Im using swi-prolog.Thanks in advance for your time and help.

推荐答案

not(boy(X)) 的问题在于 Prolog 不知道可能选择的世界"是什么对于 X 以便然后测试他们是否是男孩.boy 事实当然只知道男孩.

The problem with not(boy(X)) is that Prolog doesn't know what the "universe of possible choices" are for X in order to then test if they're a boy. The boy facts only know boys, of course.

一种方法是定义除性别之外的所有人.例如:

One approach is to define all people in addition to the gender. For example:

% Define people

person(a).
person(b).
person(c).
person(d).

% Define genders

boy(a).
boy(b).
girl(c).
girl(d).

然后检查所有非男孩",你会这样做:

Then to check for all "non-boys" you would do:

?- person(X), not(boy(X)).

根据您想要如何组织数据,您可以将性别与人相结合:

Depending upon how you want to organize your data, you can combine gender with person:

person(a, boy).
person(b, boy).
person(c, girl).
person(d, girl).

然后查询:

?- dif(Y, boy), person(X, Y).

或者把它写成谓词:

person_not_of_gender(Person, Gender) :-
    dif(OtherGender, Gender),
    person(Person, OtherGender).

然后查询:

?- person_not_of_gender(Person, boy).

这篇关于Prolog:获取一个或多个不符合事实的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:13