2
votes

I am learning Prolog for an university exam using SWI Prolog and I have some question about this simple program that implement the different predicate that say TRUE if two element are different (if they do not match) and say FALSE if they match.

This is the code:

different(X,X) :- !,
              fail.

diferent(_,_).

The problem is that if I try to execute the following query in the Prolog shell I always obtain FALSE:

[debug] 10 ?- different(a,b).
false.

[debug] 11 ?- different(a,a).
false.

As you can see the first query have to say TRUE because a don't match with b

Why?

3
Why debug when stackoverflow? - user1812457
I apologize for my stupid question...I have spent all night at work at office and I was just a litle bit in mental confusion - AndreaNobili

3 Answers

5
votes

change diferent(_,_) to different(_,_). Ie it is a spelling error. Your second predicate is not being examined as it does not match your query.

The program should be

different(X,X) :- !,fail.
different(_,_).
2
votes

This "exercise" is pointless. Use !

IMO the only reasonable definition of different/2 is:

different(A,B) :- dif(A,B).
0
votes

If you are using emacs for editing your prolog files (If not I strongly recommend you switching to it) I suggest you using hi-lock-mode. I will highlight in a pattern-like fashion all matches in the file whenever the cursor is over a word. It can save you hours of pain when you develop bigger projects.

enter image description here