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).
精彩评论