[Top][All Lists]

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

RE: "distinct" predicate

From: Robert Wolf
Subject: RE: "distinct" predicate
Date: Fri, 1 Nov 2002 09:23:58 +1030


There is a "beutiful" prolog "native" solution to "dsitinct" problem.
You can wirite it easily yourself.
Here are the hints:
1. Empty list is basically "distinct".
2. The list is "distinct" if:
        a) the "head" element is not on the "tail" list AND
        b) the "tail" list is "distinct"
3. Your dialect of prolog may already have a predicate checking if
something is on the list.
4. If not, you can write it easily yourself.
5. An element is on the list if:
        a) it is the same as the "head" element of the list OR
        b) it is on the "tail" list

Something like: (I use another dialect, so you may have to modify
syntax, sorry)

distinct ([]) :- .
distinct ([a]) :- .
distinct ([head : tail]) :-
        NOT (is_on_the_list (head, tail)),
        distinct (tail).

is_on_the_list (head, [head :tail]) :- .
is_on_the_list (head, [something :tail]) :-
        is_on_the_list (head, tail).

Best regards and "happy prologing"...

Robert P. Wolf

-----Original Message-----
From: Abhinav-Bhardwaj [mailto:address@hidden
Sent: Thursday, 31 October 2002 7:55 PM
To: address@hidden
Subject: "distinct" predicate

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]).
?- distinct([a, b, a, d]).

thanks in advance,

Users-prolog mailing list

reply via email to

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