문제

Is there a way to tell Prolog that an element in a list can be any value? I tried using _ but it didn't work. I'm trying to compare two lists of zeros and ones but in some places I don't care about the value, for example: I want [1,1,1] == [1,1,_] to return true where _ is the element I don't care about it's value

도움이 되었습니까?

해결책

Use the following instead:

[1,1,1] = [1,1,_]

==/2 tests for equivalence, which is stronger than =/2 (which instantiates).

다른 팁

Non-unifying alternative:

forall(nth1(X, [1,1,1], Val), nth1(X, [1,1,_], Val)).

Some sample input and output to show where lies the difference:

compare_lists(List1, List2) :-
     forall(nth1(Index, List1, Value), nth1(Index, List2, Value)).

test_compare(CompPred, ComparedList) :-
    call(CompPred, ComparedList, [1,1,_]).

?- test_compare(=, [X,1,1]), print(X).
1
X = 1.

?- test_compare(=@=, [X,1,1]), print(X).
false.

?- test_compare(==, [X,1,1]), print(X).
false.

?- test_compare(compare_lists, [X,1,1]), print(X).
_G648
true.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top