findall(X,P,L)
produces a list (L
) of X
for each possible solution of the goal P
. Succeeds with L
unified to an empty list if P
has no solutions.
Examples
z(r).
z(t).
z(y).
x(a,b,c).
x(q,X,e) :- z(X).
x(1,2,3).
x(w,b,c).
x(d,b,c).
x(a,b,c).
?- findall(X,x(X,Y,Z),L).
L = [a,q,q,q,1,w,d,a]
X = UNINSTANTIATED VARIABLE
Y = UNINSTANTIATED VARIABLE
Z = UNINSTANTIATED VARIABLE
yes
?- findall(X,x(X,y,z),L).
L = []
X = UNINSTANTIATED VARIABLE
yes
q(a(W)).
q(C).
q(1).
y(X) :- X = o(T,R), q(T), q(R).
?- findall(X,y(X),L).
L = [o(a(W), a(W)),o(a(W), R),o(a(W), 1),o(T, a(W)),o(T, R),o(T, 1),o(1, a(W)),o(1, R),o(1, 1)]
X = UNINSTANTIATED VARIABLE
yes
?- findall(X,y(X),L), L=[H|_], H=o(a(q),a(z)).
H = o(a(q), a(z))
L = [o(a(q), a(z)),o(a(W), R),o(a(W), 1),o(T, a(W)),o(T, R),o(T, 1),o(1, a(W)),o(1, R),o(1, 1)]
X = UNINSTANTIATED VARIABLE
yes
?- findall(Y, (member(X,[6,3,7,2,5,4,3]), X<4, Y is X*X), L).
L = [9,4,9]
X = UNINSTANTIATED VARIABLE
Y = UNINSTANTIATED VARIABLE
yes
?- findall(X,P,L).
Expected an atom or a predicate but got a VARIABLE with value: P