Reputation: 345
I'm a beginner in Prolog, and I came across a question that I couldn't understand at all. Here is the question:
When I get up in the morning and need to dress up, I follow the rules illustrated by the following diagram. It means that before putting my right shoe on I need to put on my pants and my right sock, and so on. I don’t show arrow heads on the diagram – if there is a line between nodes a and b I assume that it is oriented from a to b iff b is above a in the diagram. Thus, my figure can be treated as the directed graph of a relation, before.
Create a knowledge base of facts that represent the Hasse diagram. Use a predicate before/2 (this notation is accepted in the Prolog literature and means that the predicate before has two arguments).
Upvotes: 1
Views: 409
Reputation: 5858
one encoding would be:
before(ItemA, ListOfAllItemsYouHaveToWearBeforeItemA).
or before(ListOfAllItemsYouHaveToWearBeforeItemA,ItemA).
(i think that the first is better)
so for the right shoe you would write
before(right_shoe,[right_sock,pants]).
another possible encoding would be before(ItemA,ItemYouHaveToWearBeforeItemA).
(or reverse).
in that case you would have to write something like:
before(right_shoe,right_sock).
before(right_show,pants).
the choice of the encoding depends on what you want/need to do next (or any other exercise requirements). for example the first encoding would be better if you want to get the requirements for one item while the second would be better if you want to not only ask what you should wear before an item but also what you could wear after an item.
if I had just to represent the graph i would go with the second encoding because both arguments have the same type (graph items) and it's a more direct representation of the graph
Upvotes: 3