0

I want to write the rule unique(List) that checks if all elements in List are unique. I'm not allowed to use member, but I am allowed to use 'not'. Therefore I wrote the rule 'member' myself.

I wrote this:

member(Element, [Element|_]).
member(Element, [_|List]) :-
    member(Element, List).

unique([H|T]) :-
    not(in_list(H, T)),
    unique(T).

The member-rule is working, it checks if Element is a member of List. But the unique- rule doesn't work. For the unique-rule is was expecting it would check if H was in T and then do the same for the Header of the Tail and so on. The 'not' makes from a false statement a True-output. When I run this rule with query ?-unique([1,2,3,4,5]) it gives False. So what's my mistake?

false
  • 10,264
  • 13
  • 101
  • 209
jack
  • 1
  • `all_dif` at https://stackoverflow.com/a/31724022/ will work correctly also with an open list, due to using `freeze`: ?- all_dif(L), L = [A,B|T], T = [C,D]. – brebs Dec 30 '22 at 15:51
  • See https://stackoverflow.com/questions/20131904/check-if-all-numbers-in-a-list-are-different-in-prolog – brebs Dec 30 '22 at 17:16

1 Answers1

-1

I just discoverd the solutions a few moments later. I added this above:

unique([]).
unique([_,[]]).
jack
  • 1
  • Consider `?- unique([[],[]]).` which should fail, but succeeds with this definition. – false Dec 31 '22 at 06:27
  • @ jack, you meant `unique([_|[]]).` which is just `unique([_]).`. you also meant to name your "`member`" predicate "`is_list`". – Will Ness Jan 01 '23 at 20:06