2
votes

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.
A contains D, E and F. (Something like A and B is D, A and C is E) F contains A without D and E.
And also elements in list F are in pairs (example if A contains [a,b,c] then F would contain [a,a,b,b,c,c].)

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]  
2
add a complete example. When should your predicate be true for instance?! - aioobe
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,f] - Kaspars
but F should contains "pairs"? - aioobe
Yes, but how should A, B, C, D, E and F get their values. Is it testing, if so what is to be tested? Or is it building, again if so what is to be built? There must be some rules for it. what are they? - rvirding

2 Answers

4
votes

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).
0
votes

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.