序言中的匹配变量
我在项目中需要创建一个名为is_category(c)的谓词,该谓词检查该c是否存在于我的数据库中,这是一个可以澄清的示例:
is_category(animals):-
true.
另一个:
is_category(C):-
C= animals;
C= animals;
C= greetings;
C= fruits;
C= animals;
C= collections.
我的事实是:
word(horse,animals).
word(panda,animals).
word(hello,greetings).
word(banana,fruits).
word(bison,animals).
word(hoard,collections).
现在我的问题是如何处理。我想到的唯一解决方案就是要问是否有任何类别命名动物并使用钥匙切割,它显示一个真实的方法,但它仅显示动物一词并停止,因此请帮助我。
这是我的想法:
is_category(C):-
word(_,C),!.
I have a requirement in my project that I create a predicate named is_category(C), which checks if this C exists in my database or not here is an example to clarify:
is_category(animals):-
true.
Another one:
is_category(C):-
C= animals;
C= animals;
C= greetings;
C= fruits;
C= animals;
C= collections.
And my facts are:
word(horse,animals).
word(panda,animals).
word(hello,greetings).
word(banana,fruits).
word(bison,animals).
word(hoard,collections).
Now my question is how to do this requirement as my only solution I thought about was to just ask if there were any category named animals and use the key cut, it shows one true but it only shows the word animals and stop so please assist me if you can.
Here is my thought:
is_category(C):-
word(_,C),!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用这样的谓词:
因此,对于这个知识库:
您可以问“有一个类别动物的单词吗?”
在回溯时,会生成所有可能的不同(和分类)类别:
You can use predicate like this:
So for this knowledge base:
You can ask "Is there a word with category animal?"
And on backtracking generate all possible distinct (and sorted) categories: