문제

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:4 without 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:4 is the same as (_G1-2):(3:4), not _G1-(2:3:4), and so the unification of _G1-2:3:4 and X-_ fails.

If you add parentheses to your terms, like _G1-(2:3:4), then your original remove_position/2 predicate will work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top