views:

63

answers:

3
+1  Q: 

Prolog wildcards

Hi guys,

I was wondering, how would I use the underscore twice but check that both instances of that underscore unify?

What I basically need is something that returns true if two elements of the same value in one mapping exist...

I.E member((,),[(a,a),(b,a),(c,a)]).

If I use a variable does that make them unified?

I.E member((A,A),[(a,a),(b,a),(c,a)]).

But it returns the variable rather than true.

I need some enlightenment.

Thanks

+5  A: 

Your solution with a variable is correct.

Returning a variable is a way to return true. It really means: this goal is true when var = value, as opposed to this goal is true.

Note that using this as a clause in a different predicate will hide the variable:

contains_identical_pair(List) :- member((A,A),List).
Jerome
+2  A: 

You can use double negation to avoid variable bindings:

?- \+ \+ member((A,A),[(a,a),(b,a),(c,a)]).
true.
starblue
that's pretty fancy ;)
sharky
+2  A: 

The bindings for the variables printed on the prolog screen are just there to make life easier in an interactive prompt so that you don't have to print out the variables you care about each time. They don't affect the logic of your code at all.

They will only be printed for variables are are entered at the prompt. So if the predicate you are writing is part of a larger program, you can just ignore this output, or if you want this to be a top-level predicate that people will call from the prompt and you don't want the output printed, then simply wrap your call in a predicate that has no arguments or has only input arguments. ie:

wrapper :-  
    predicate(Out1,Out2).

or:

wrapper(In1,In2) :-
    predicate(In1,In2,Out1,Out2).
humble coffee