users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "distinct" predicate


From: Emmanuel Coquery
Subject: Re: "distinct" predicate
Date: Thu, 31 Oct 2002 10:24:52 +0100

Le Jeudi 31 Octobre 2002 10:24, Abhinav-Bhardwaj a écrit :
> hi,
> is there any way by which i can specify a rule say
> distinct..which succeeds only when all the terms in a list
> are distinct.
> for eg:
> ?- distinct([a, b, c, d]).
> yes
> ?- distinct([a, b, a, d]).
> no
>

You can define it by 

distinct(L) :- sort(L,L2), length(L,N), length(L2,N).

when sorting, sort/2 regroups duplicates. So if L and L2 don't have the same 
length, it means that L was containing duplicates. This also works with 
variables:

| ?- distinct([X,Y,X]).
no
| ?- distinct([X,Y]).
yes

regards
        
        Emmanuel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]