The following codes give these results:
?- X = a, findall(Element, ( member(Z, [a,b,c]), Element = Z:X ), Set).
X = a,
Set = [a:a, b:a, c:a].
But when I want that all elements will share the same unbound variable (instead of a), then things are not working as it is supposed:
?- X = Y, findall(Element, ( member(Z, [a,b,c]), Element = Z:X ), Set).
X = Y,
Set = [a:_G1918, b:_G1912, c:_G1906].
Why _G1918, _G1912, and _G1906 are not bound to each other? is that a bug in swi-prolog?
You can use
bagof/3
for that:From SWI-Prolog's documentation:
In your query
X
is a free variable, so the result you get is the same with the one forbagof/3
withX^
: