Member Code / 2 with some determinism

How can I enter the / 2 code, which has determinism for the last element. I am currently using:

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

When I request the following:

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

The interpreter continues the search after returning 2, since there is still a choice point on the left. How can I implement the / 2 element so that this will no longer be?

But the full semantics of the / 2 term must be preserved, i.e. answers for example:

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

Should still work as a befor.

Bye

+4
source share
1 answer
 member(B, [C|A]) :- member_(A, B, C). member_(_, A, A). member_([C|A], B, _) :- member_(A, B, C). 

It is the result of two calls to lists in swi.

+4
source

Source: https://habr.com/ru/post/1385414/


All Articles