Preventing Variables from Listing

I am trying to write a predicate that returns 1 if the variable Xand the predicate f(X)are both elements of the input list L, and 0 if at least one of them is missing.

This is what the predicate should do:

?- f_in_list([X, f(X)], Val).
  should return Val = 1

?- f_in_list([X, f(Y), Z], Val).
  should return Val = 0, as X and Y are different variables.

I wrote this simple code:

f_in_list(L, 1) :-
    member(X, L),
    member(f(X), L),
    !.
f_in_list(_, 0).

My problem is that Prolog is always trying to unify input variables, so he returns X = f(X)and X = f(Y), respectively.
I tried to use dif(X, f(X))to get around this problem, but even this did not work. Valthere will always be 1 if the list contains at least two elements.

, Prolog ? , ?

+4
2

==/2. , , . ==/2:

2 ?- X == X
|    .
true.

3 ?- X == Y.
false.

4 ?- X = Y, Z = Y, X == Z, write('yes'), nl.
yes
X = Y, Y = Z.

5 ?- X = Y, Z = W, X == Z.
false.

6 ?-

nu_member/2:

nu_member(X, [Y|_]) :- X == Y.
nu_member(X, [_|T]) :- nu_member(X, T).

f_in_list/2:

f_in_list(L, 1) :-
    member(X, L),
    nu_member(f(X), L),
    !.
f_in_list(_, 0).

:

2 ?- f_in_list([X,f(X), Z], B).
B = 1.

3 ?- f_in_list([X,f(Y), Z], B).
B = 0.

4 ?- f_in_list([X,f(Y), f(Z)], B).
B = 0.

5 ?- f_in_list([X,f(Y), f(Z),Z], B).
B = 1.
+1

@lurker, ==/2, , .

:

f_in_list(L, 1) :-
    member(X, L),
    member(f(Y), L),
    X == Y ,!.

f_in_list(_, 0).

:

?- f_in_list([X, f(X)], Val).
Val = 1.

?- f_in_list([X, f(Y), Z], Val).
Val = 0.
+3
source

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


All Articles