这是我正在工作的知识库:
localLib('AHorowitz','Stormbreaker',2).
localLib('AHorowitz','Scorpia',4).
localLib('AHorowitz','Ark Angel',6).知识库的关键如下:
localLib(W,B,C) where
W=Writer
B=Book
C=Acknowledgements--我想写一条规则,把作者的所有致谢加在一起。
getAcknowledgement(W,X):- findall(C,localLib(W,_,C),X).这条规则帮助我在单独的列表中列出所有的致谢。
?- getAcknowledgement('AHorowitz',X).
X = [2, 4, 6]我现在被困在如何添加这些项目。我知道内置的sum_list,尽管我知道这是不正确的,但我想要实现的是:
getAcknowledgement(W,X):- findall(C,localLib(W,_,C),X).
sum_list(X,[getAcknowledgement]).
/* I would like to sum the output that I receive from the first rule above.
The KB has been simplified in this example to 3 clauses however in reality
there are 1000.*/我该怎么做,有什么帮助吗?
发布于 2015-01-09 18:16:11
听起来好像你想要找到作者的致谢数。
bagof/3是你在这里的朋友。它
bagof(+Template, :Goal, -Bag)
将袋与模板的替代品统一起来。如果目标除了一个与模板共享的变量外,还有自由变量,bagof/3会回溯这些自由变量的备选方案,将包与相应的模板方案统一起来。构造+Var^Goal告诉bagof/3不要在目标中绑定Var。如果目标没有解决方案,bagof/3就会失败。findall/3是
等价于与存在运算符(
bagof/3)绑定的所有自由变量(^)的bagof/3,但当目标没有解决方案时,bagof/3失败。
So...this应该为给定的作者获取知识的总和,或者,如果Writer不受约束,在回溯时,它将为所有作者找到解决方案,一次一个。
acknowledgements_by_writer( Writer , Acknowledgements ) :-
bagof( N , local_lib(Writer,_,N) , Ns ) ,
sum_list(Ns,Acknowledgments).如果你想要感谢的总数,这样的事情应该对你有帮助。
total_acknowledgements(T) :- findall(N,local_lib(,,N),Ns),sum_list(Ns,T)。
https://stackoverflow.com/questions/27844039
复制相似问题