predicate with Prolog
I have to write a program which has a predicate p(A,B,C,D,E,F)
. A,B,C,D,E,F are lists.
So.. I can't get where to start. I have read tutorial and still.. I don't quite get it.
example:
A is [a,b,c,d,e,f,g]
B is [a,c,d,q,w]
C is [e,d,g,m,n]
D is [a,c,d]
E is [e,d,g]
F is [b,b,f,f]
A contains D, E and F
Sounds like you would want to have a look at member
and append
.
Something like A and B is D, A and C is E
This could probably be solved with something similar to append(A, B, D)
("D
is the concatenation of A
and B
") and append(A, C, E)
("E
is the concatenation of A
and C
").
F contains A without D and E.
Use append
to find out what F
is. Something like append(FwithoutPairs, A, DandE)
and then create a pairsOf
predicate, that looks something like
pairsOf([], []).
pairsOf([H, H | T], [H | S]) :- pairsOf(T, S).
In addition to aioobe's answer :
Something like A and B is D, A and C is E
p(A,B,C,D,E,_F):-
intersection(A,B,D),
intersection(A,C,E).
You would eventually need to order the lists but intersection/3
seems more accurate.
精彩评论