Reputation: 53
Suppose we are given certain facts and we were supposed to write a certain rule that explores a given condition in those facts, for example, if we are given certain climates and their characteristics:
climate(jun, 20, snow, wind, cold).
climate(jul, 20, hot, dry, calm).
climate(sep, 15, calm, dry, cool).
climate(sep, 16, rain, hot, calm).
climate(sep, 18, warm, rain, calm).
climate(oct, 29, cool, dry, calm).
climate(nov, 1, cold, snow, wind).
climate(nov, 5, hot, snow, wind).
climate(dec, 15, warm, rain, wind).
climate(dec, 21, wind, cold, dry).
If we wanted to find out which climates were hot and dry, would we do something like this:
climatecheck(Month) :-
climate(Month, _, hot, _, _);
climate(Month, _, _, hot, _);
climate(Month, _, _, _, hot),
climate(Month, _, dry, _, _);
climate(Month, _, _, dry, _);
climate(Month, _, _, _, dry).
I tried the method above but it doesnt seem to work.
Instead of giving me:
Month = jul.
It gives me all the months which have EITHER hot or dry climates.
What could I be doing wrong, despite outlining specific positions of each characteristic with "and" and "or" conditionals? Thank you.
Upvotes: 2
Views: 443
Reputation: 726987
Use parentheses to force the precedence in evaluating your predicate:
climatecheck(Month) :-
( climate(Month, _, hot, _, _)
; climate(Month, _, _, hot, _)
; climate(Month, _, _, _, hot)
),
( climate(Month, _, dry, _, _)
; climate(Month, _, _, dry, _)
; climate(Month, _, _, _, dry)
).
Upvotes: 3
Reputation: 60034
climatecheck(Month) :-
climate(Month, _, X, Y, Z),
% overkill generalization ahead :)
maplist([P]>>memberchk(P, [X,Y,Z]), [hot, dry]).
?- climatecheck(Month).
Month = jul ;
false.
Upvotes: 2