Reputation: 415
I would like to write a predicate different_from(Xs,X) which is a check that succeeds if and only if X is different from all the elements of the list Xs.
So the query
different_from([3,2,5],4)
should succeed but this following query should fail:
different_from([3,2,5],2)
Since the predicate is a check it should not instantiate either of its arguments.
thank you in advance for any help you can provide.
Upvotes: 1
Views: 1205
Reputation: 726479
This is a very simple rule consisting of two clauses:
different_from
is successful when the list is empty,different_from
is successful when the first element of the list does not match the element being searched, and also when different_from
for the tail is successful as well.Here is the same thing written in Prolog syntax:
different_from([], _).
different_from([H|T], E) :- E \= H, different_from(T, E).
Upvotes: 2