Arashicage
Arashicage

Reputation: 25

How do I compare variables following a pattern defined by the user?

My predicate needs to compare variables according to the pattern set by the user.

For example :

p1([A,A,B,A],[1,1,5,1]).
true

p1([A,A,B,C],[1,1,1,5]).
false

p1([A,B,B,A,C],[2,3,3,2,1]).
true

etc.

For now my code only works for 2 variables. I don't know how to make the whole thing recursive.

isEqual(A,A).

pattern([A],X):-isEqual(A,X).
pattern([A,A],X):-isEqual(A,A),isEqual(X,[N1,N2]),isEqual(N1,N2).
pattern([A,B],X):-isEqual(X,[N1,N2]),not(isEqual(N1,N2)),A\==B.

Upvotes: 1

Views: 50

Answers (1)

false
false

Reputation: 10102

This predicate is inherently impure. What seems to be intended is that all variables in the pattern are different.

pattern_instance(Varpattern, Instance) :-
   term_variables(Varpattern, Vs),
   \+ \+ ( Varpattern = Instance, alldifferent(Vs) ).

alldifferent([]).
alldifferent([X|Xs]) :-
   maplist(dif(X), Xs),
   alldifferent(Xs).

Upvotes: 1

Related Questions