The prologue forgot / didn’t set my variable?

I am very new to Prolog and I have a problem when my variable seems to be forgotten

test(S) :-
X = 'testing',
(S = y, write(X) );
(S = n, write(X) ).

Launch

test(y)

Prints the text as expected, but

test(n)

displays

_L160

What do I suppose means that the variable is not constant? Why is this happening?

I know that he can spit on two predicates, for example:

test(y) :- X = 'testing', write(X).
test(n) :- X = 'testing', write(X).

but my actual problem is a much larger predicate that cannot be simplified in this way.

+3
source share
3 answers

Disjunction (;) is currently being performed either:

  • Assign 'testing' to X, y to S and write X. OR, if that fails, backtrace and
  • Assign n to S and write X

Add a few parentheses to make them work as intended.

test(S):-
X = 'testing',
    (
        (S = y, write(X) )
    ; 
        (S = n, write(X) )
    ).
+2
source

- ;/2. listing(test/1), :

 test(A) :-
    (   B=testing,
        A=y,
        write(B)
    ;   A=n,
        write(B)
    ).

, .

, , :

test(S) :-
   X = 'testing',
   ((S = y, write(X) );
    (S = n, write(X) )).
+3

:

test(S) :-
        (   X = 'testing', S = y, write(X)
        ;   S = n, write(X)
        ).

, , .

+2

All Articles