I am trying to find the intersection of two lists in Prolog using the intersection()
function. Unfortunately, my code is giving undesirable results.
Here is what I have:
RESULTA = [ ('10:00 am - 11:15 am', 'TR'), ('6:00 pm - 8:50 pm', 'T'), ('6:00 pm - 8:50 pm', 'R'), ('6:00 pm - 8:50 pm', 'M')].
RESULTB = [ ('6:00 pm - 8:50 pm', 'R'), ('3:00 pm - 3:50 pm', 'TR')].
intersection(RESULTA, RESULTB, AB).
The desired output is ('6:00 pm - 8:50 pm', 'R')
however, the code returns RESULTA = AB, AB = [].
Does anyone have an idea on how to fix this issue?
EDIT
RESULTA and RESULTB are actually generated from bagof() operations. My actual code is:
1 ?- bagof((TIME, DAYS), COURSE^teaches_at('Dr. J. Leidig', COURSE, TIME, DAYS), RESULTA).
RESULTA = [ ('10:00 am - 11:15 am', 'TR'), ('6:00 pm - 8:50 pm', 'T'), ('6:00 pm - 8:50 pm', 'R'), ('6:00 pm - 8:50 pm', 'M')].
2 ?- bagof((TIME, DAYS), COURSE^teaches_at('Dr. El-Said', COURSE, TIME, DAYS), RESULTB).
RESULTB = [ ('6:00 pm - 8:50 pm', 'R'), ('3:00 pm - 3:50 pm', 'TR')
Sorry for the confusion.
The code in your question is really queries to Prolog.
There are 3 different queries: each query ends with a period. All three queries are completely independent, even if they share variables with same names.
To do what you want just change periods to commas:
RESULTA = [ ('10:00 am - 11:15 am', 'TR'), ('6:00 pm - 8:50 pm', 'T'), ('6:00 pm - 8:50 pm', 'R'), ('6:00 pm - 8:50 pm', 'M')],
RESULTB = [ ('6:00 pm - 8:50 pm', 'R'), ('3:00 pm - 3:50 pm', 'TR')],
intersection(RESULTA, RESULTB, AB).
Update after the question updated:
bagof((TIME, DAYS), COURSE^teaches_at('Dr. J. Leidig', COURSE, TIME, DAYS), RESULTA),
bagof((TIME, DAYS), COURSE^teaches_at('Dr. El-Said', COURSE, TIME, DAYS), RESULTB),
intersection(RESULTA, RESULTB, AB).
you must use that variables in a rule, like
test(AB) :-
RESULTA = [ ('10:00 am - 11:15 am', 'TR'), ('6:00 pm - 8:50 pm', 'T'), ('6:00 pm - 8:50 pm', 'R'), ('6:00 pm - 8:50 pm', 'M')],
RESULTB = [ ('6:00 pm - 8:50 pm', 'R'), ('3:00 pm - 3:50 pm', 'TR')],
intersection(RESULTA, RESULTB, AB).
then consult the file. The you'll get
?- test(AB).
AB = [ ('6:00 pm - 8:50 pm', 'R')].
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.