Reputation: 3262
I want to build a Prolog program to determine if two lists, provided as arguments, are not equal.
This is what I did so far.
not-equal([],[a|_]).
not-equal([a|_],[H|T]):-not-equal(a,T).
Upvotes: 3
Views: 10525
Reputation: 9282
Prolog lists are just terms that can be "compared" directly using the equality operators. Not equal can mean not unifiable or not identical.
not unifiable
?- [1, 2] \= [1, 2]. ===> false
?- [1, 2] \= [1, X]. ===> false
?- [1, 2] \= [1, 3]. ===> true
not identical
?- [1, 2] \== [1, 2]. ===> false
?- [1, 2] \== [1, X]. ===> true
?- [1, 2] \== [1, 3]. ===> true
Upvotes: 8
Reputation: 117175
Here's what I came up with:
not-equal([],[H|_]).
not-equal([H|_],[]).
not-equal([H|T1],[H|T2]) :-
not-equal(T1,T2).
not-equal([H1|T1],[H2|T2]) :-
not(var(H1)),
not(var(H2)),
H1 =\= H2.
In your predicates you have a lowercase 'a' which is an atom and not a variable. Also when you call not-equal(a,T)
you are going outside of using lists so it's not going to work.
I'd also consider changing the name of the predicate to not-unifiable
as the the list may contain variables that could make them equal or not depending on how those variables are unified in the future.
There are still cases that don't work with my above code.
As an alternative I would consider using the ?=
operator instead, like this:
not-equal([H1|T1],[H2|T2]) :- not([H1|T1]?=[H2|T2]).
Let me know if these help.
Upvotes: 0