Question

How can I code member/2 that has determinism for the last element. Currently I am using:

member(X,[X|_]).
member(X,[_|Y]) :- member(X,Y).

When I query the following:

?- member(X,[1,2]).
X = 1 ;
X = 2 ;
No

The interpreter continues searching after returning 2 since there is still a choice point left. How could I implement member/2 so that this does not happen anymore?

But the full semantic of member/2 should be preserved, i.e. answers such as:

 ?- member(X,Y)
 Y = [X|_1] ;
 Y = [_1,X|_2] ;
 etc..

Should still work as befor.

Bye

Was it helpful?

Solution

member(B, [C|A]) :-
    member_(A, B, C).
member_(_, A, A).
member_([C|A], B, _) :-
    member_(A, B, C).

Is the result of two listing calls on swi.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top