Prolog findall/3

血红的双手。 提交于 2019-12-02 02:06:03

问题


Say I have a predicate pred containing several facts.

pred(a, b, c).
pred(a, d, f).
pred(x, y, z).

Can I use findall/3 to get a list of all facts which can be pattern matched?

for example, if I have

pred(a, _, _) I would like to obtain

[pred(a, b, c), pred(a, d, f)]


回答1:


Just summing up what @mbratch said in the comment section:

Yes, but you have to make sure that you either use named variables or construct a simple helper predicate that does that for you:

Named variables:

findall(pred(a,X,Y),pred(a,X,Y),List).

Helper predicate:

special_findall(X,List):-findall(X,X,List).

?-special_findall(pred(a,_,_),List).
List = [pred(a, b, c), pred(a, d, f)].

Note that this doesn't work:

findall(pred(a,_,_),pred(a,_,_),List).

Because it is equivalent to

 findall(pred(a,A,B),pred(a,C,D),List).

And thus doesn't unify the Variables of Template with those of Goal.



来源:https://stackoverflow.com/questions/21082855/prolog-findall-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!