简体   繁体   English

事实序言

[英]Prolog list of facts

I would like to create a list of all the exam(A,B) contained in the knowledge base: 我想创建知识库中包含的所有考试(A,B)的列表:

exam([a, 26]).
exam([b, 30]).
exam([c, 20]).
exam([d, 19]).

I know that i can simply use: 我知道我可以简单地使用:

exams(L) :- findall(X, exam(X), L).

But since i want to write my own roles, i wrote the following: 但是由于我想编写自己的角色,因此编写了以下内容:

exams([exam([A,B])]) :- exam([A,B]).
exams([exam([A,B])|Ex]) :- \+(member(exam([A,B]),Ex)), exams(Ex).

Why with a query like ?- exams(X). 为什么要使用?-exams(X)之类的查询 the system answers as follow: 系统回答如下:

?- exams(X).
X = [exam([a, 26])] ;
X = [exam([b, 30])] ;
X = [exam([c, 20])] ;
X = [exam([d, 19])] ;
false.

And doesn't give me any list with more than just one exam, also if the following query returns true: 而且,如果以下查询返回true,则不会列出任何一项考试不止一次的清单:

?- exams([exam([a,26]),exam([b,30])]).
true .

a possibility is 一种可能性是

exams(L) :- exams([], R), !, reverse(R, L).

exams(S, L) :- exam(E), \+ memberchk(exam(E), S), exams([exam(E)|S], L).
exams(L, L).

but, apart learning about Prolog control flow, I cannot see any value in such solution. 但是,除了学习Prolog控制流程之外,我看不到这种解决方案有任何价值。 Maybe you should elaborate the reason that force you to avoid findall/3... 也许您应该详细说明迫使您避免使用findall / 3的原因...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM