So here it is, for my sudoku project I have a list, such as :
L = [_G1-0:0:0,_G19-0:6:2,_G22-0:7:2,_G25-0:8:2].
and I want to filter this to get only the free variables :
[_G1, _G19, _G22 and _G25].
So wanting that, I wrote this predicate :
remove_position([], []) :- !.
remove_position([X-_|Xs], [X|Rs]) :- remove_position(Xs, Rs).
When calling
?- remove_position(L, Result).
SWI-Prolog answers
false.
So I edited the predicate a little :
remove_position([], []) :- !.
remove_position([X-_:_:_|Xs], [X|Rs]) :- remove_position(Xs, Rs).
and now it works as expected :
?- remove_position(L, Result).
Result = [_G1, _G19, _G22, _G25].
Still, I can’t see what’s wrong with the first one, for me, SWI-Prolog should match 0:0:0 with _ just as well as with _:_:_. Is there something I do not get ? Or is it a SWI-Prolog bug ?
Thanks for any enlightenment !
When you have a term
_G1-2:3:4without any parentheses, you have to consider the associativity and precedence of the operators in order to find out how the term is parsed.The term
_G1-2:3:4is the same as(_G1-2):(3:4), not_G1-(2:3:4), and so the unification of_G1-2:3:4andX-_fails.If you add parentheses to your terms, like
_G1-(2:3:4), then your originalremove_position/2predicate will work.