TAGS :Viewed: 17 - Published at: a few seconds ago

[ how to find the matching element in prolog list ]

I have following facts in prolog.

p(cold,[flu,not high_body_temp,headache,dizzy],0.2],
p(cold,[flu,high_body_temp,not headache,dizzy],0.4],
p(cold,[flu,high_body_temp,headache,not dizzy],0.1],
p(cold,[not flu,high_body_temp,headache,dizzy],0.3],
p(cold,[flu,not high_body_temp,headache,not dizzy],0.3],

p(diarrhea,[headache,not stomachache,dizzy,vomit],0.5),
p(diarrhea,[headache,stomachache,not dizzy,vomit],0.4),
p(diarrhea,[headache,stomachache,dizzy,not vomit],0.2),
p(diarrhea,[not headache,stomachache,dizzy,vomit],0.1),
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1),

And a list that is generated at runtime, for example:


we should get the answer from two facts contain 'true' elements of [flu,headache] whereas 'not' means the element is not exist:

p(cold,[flu,not high_body_temp,headache,not dizzy],0.3],
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1),

and the answer should be:

cold = 0.3
diarrhea = 0.1

How can i write a code in prolog to get this done?please help. i'm totally stuck. TQ.

Answer 1

once corrected the syntax errors, seems a very simple matching:

:- op(100, fx, not).

p(cold,[flu,not high_body_temp,headache,dizzy],0.2).
p(cold,[flu,high_body_temp,not headache,dizzy],0.4).
p(cold,[flu,high_body_temp,headache,not dizzy],0.1).
p(cold,[not flu,high_body_temp,headache,dizzy],0.3).
p(cold,[flu,not high_body_temp,headache,not dizzy],0.3).

p(diarrhea,[headache,not stomachache,dizzy,vomit],0.5).
p(diarrhea,[headache,stomachache,not dizzy,vomit],0.4).
p(diarrhea,[headache,stomachache,dizzy,not vomit],0.2).
p(diarrhea,[not headache,stomachache,dizzy,vomit],0.1).
p(diarrhea,[headache,not stomachache,not dizzy,not vomit],0.1).

answer(L, A, N) :-
    p(A, S, N), maplist(match(L), S).
match(L, not S) :-
    \+ memberchk(S, L).
match(L, S) :-
    memberchk(S, L).


10 ?- answer([flu,headache],A,N).
A = cold,
N = 0.3 ;
A = diarrhea,
N = 0.1 ;