开发者

predicate with Prolog

开发者 https://www.devze.com 2023-01-26 20:11 出处:网络
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 with开发者_C百科out D and E.

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 with开发者_C百科out 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消