I am new in Prolog and I would like to apply XOR operation on elements of given list of length n. The predicate should return True if list contains some false elements in the first n-1 element or if last element is True.
I have written the following code so far, but it does not work properly such as for the query ?- function([true,false,false]) the predicate should return True but it returns false.
function([X|_]) :- \ + X,!.
function([X]):-X,!.
function([_|XS]):- function(XS),!,helper(XS).
helper([X]):- X,!.
helper([_|YS]):- helper(YS),!.
I would appreciate if you could help me. Thanks!
Here's your spec again:
Let's define predicate
xor_check/1
like this:Above code is based on
xor_check__aux/2
, which in turn builds onmemberd/2
:The auxiliary predicates
boolean/1
andbooleans/1
can be defined as:Sample queries (with SICStus Prolog 4.3.2):