Prolog findall / 3

Say I have a predicate predicate containing a few facts.

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

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

for example if i have

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

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

+2
source share
1 answer

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

Yes, but you must make sure that you either use named variables or create a simple helper predicate that does this 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)].

Please note that this one does not work :

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

Since it is equivalent

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

, , Template Goal.

+2

All Articles