Prolog Beginner: How to make unique values for each Variable in a predicate.

Posted by sixtyfootersdude on Stack Overflow See other posts from Stack Overflow or by sixtyfootersdude
Published on 2010-06-06T12:02:45Z Indexed on 2010/06/06 12:12 UTC
Read the original article Hit count: 284

Filed under:
|
|
|

I have a prolog predicate:

DoStuff( [A|B] ) :-
   <Stuff that I do>
   ...
   </Stuff that I do>

It is all done except it needs to do return unique values. Ie if you do:

?- DoStuff(A,B,C,D).

it should return:

A=1;
B=2;
C=3;
D=4. 

(Or something similar, the key point is that all of the values are unique).

However you should be able to do this too:

?- DoStuff(A,A,B,B).

And still get a valid answer. Ie:

A=1;
B=2.

How can I do this? What I was planning on doing was something like this:

DoStuff( [A|B] ) :-
   <Stuff that I do>
   ...
   </Stuff that I do>
   unique([A|B]).

unique([]).
unique([A|B]) :-
    A is not B.  

However I think that will make DoStuff([A,A,B]) not work because not all values will be unique.

© Stack Overflow or respective owner

Related posts about beginner

Related posts about prolog