Prolog: Simple date range selection rule

2019-08-17 16:52发布

问题:

I am a bit stuck on this question - Write a rule where given two people A and B and a date D returns the list of all dates of when A and B have exchanged messages until date D.

I have this so far but I don't know where I am going wrong.

mexchanged(A,B,D) :- message(A,B,D1), D > D1. 

And secondly, I am completely stuck on this - Write a rule that counts the number of messages sent for each person in a database of facts.

How do I write these rules?

edit: sorry I forgot to post these before;, these are few of the facts.

message(ben, tom, '20-03-2011').
message(kim, james, '17-11-2011').
message(ben, kim, '06-12-2011').
message(harry, jacky, '29-09-2011').
message(barry, chin, '06-01-2011').

回答1:

You are actually not that far off, except I have no idea what G is representing here:

message(hunter, user3147584, '12-30-2013').
message(hunter, mom,         '12-29-2013').
message(hunter, user3147584, '12-28-2013').

% predicate to test for exchanged messages
mexchanged(A,B,D) :- message(A,B,D1), D @> D1 ; 
                     message(B,A,D1), D @> D1 .

% example
mexchanged(hunter, user3147584, '12-29-2013').
% output = true


标签: prolog