Why is the result not just true, but true and then false?
The definition of member/2
may succeed further times. To decide this, it is necessary to explore the remaining list. This costs in proportion to the size of (the longest list prefix of) the list argument. Consider:
?- member(1,[1,2,3|Es]).
true
; Es = [1|_A]
; Es = [_A,1|_B]
; Es = [_A,_B,1|_C]
; ... .
?- member(E,[1,2,3|Es]).
E = 1
; E = 2
; E = 3
; Es = [E|_A]
; Es = [_A,E|_B]
; Es = [_A,_B,E|_C]
; ... .
Both questions produce redundancy in their answers. The first answer together with the 2nd Es = [1|_A]
(in the first question) and the 4th Es = [E|_A]
(in the second question) may hold simultaneously.
?- member(1,[1,2,3,1]).
true
; true. % same as 1st answer
?- member(E,[1,2,3,1]).
E = 1
; E = 2
; E = 3
; E = 1. % same as 1st answer
A clean way out of this situation is to use a better definition of member/2
called memberd/2
.
?- memberd(1,[1,2,3,1]).
true.
?- memberd(1,[1,2,3,1|Es]).
true.
?- memberd(E,[1,2,3,1]).
E = 1
; E = 2
; E = 3
; false.
?- memberd(E,[1,2,3|Es]).
E = 1
; E = 2
; E = 3
; Es = [E|_A], dif:dif(E,1), dif:dif(E,2), dif:dif(E,3)
; Es = [_A,E|_B], dif:dif(E,_A), dif:dif(E,1), dif:dif(E,2), dif:dif(E,3)
; ... .
The price for this definition is that constraints dif/2
are needed to exclude redundancies in certain situations.