简体   繁体   中英

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.
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]  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM