开发者

finding exact variable in tho list on prolog

I think this question must be solved :))

concert(dtatu, [jack, volker, rachel]).

concert(tmegadeth, [volker, rachel]).

concert(ssoad, [kurt, rachel]).

concert(trbeyonce, [kurt,jack,volker]).

I want to implement the predicate 开发者_开发知识库audiance(L1, L2). returns the list of all people(L2) who have watched all the concerts in L1. The lists L1 and L2 must not contain any duplicates.

audiance([tatu,beyonce],X). returns X=[jack,volker]

audiance(X,[volker,rachel]). returns X=[tatu,megadeth]

audiance(X,[kurt,volker,rachel])

audiance([tatu, beyonce],[jack, volker]) returns true

audiance(X,Y). returns X=[tatu], Y=[jack, volker, rachel]; X=[megadeth, soad], Y=[rachel];...

this is very hard for me. But I think If I know how to do it, I would not have any problem with prolog :)))


To avoid duplication, first you can extract all performers from the clause database and find corresponding audiences for a specific set of performers. A solution could be as follows:

subset([], []).
subset(Xs, [_|Ys]) :- subset(Xs, Ys).
subset([X|Xs], [X|Ys]) :- subset(Xs, Ys).

allPerformers(Ts) :- findall(T, concert(T, _), Ts).
performers(T) :- allPerformers(Ts), subset(T, Ts).

audience1([T], L) :- concert(T, L).
audience1([T|Ts], L) :- concert(T, L0), audience1(Ts, L1), intersection(L0, L1, L).

audience(X, Y) :- performers(X), audience1(X, Y).
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜