“Or” procedure in prolog

。_饼干妹妹 提交于 2019-12-29 02:08:08

问题


I'm doing a prolog program for college that is a bit like the cluedo game. I have six suspects with different traits:

suspect(Name, Age, Weapon, Shape, Object, Shoes)

The goal is to implement a series of clues so that the program says which the different traits of all the six suspects. For example:

suspect(Hannibal Lecter,67,knife,'in good shape',mac,'high heels')

I'm having problems trying to implement the clue

or(suspect1, suspect2, suspect3, listOfSuspects)

This clue is supposed to say that the suspect1 has the same traits as suspect2 OR as suspect3 but not both. Example: To indicate that the suspect who is 50 years old has a ring or a mac, but not both:

or(suspect(_, 50, _, _, _, _),
   suspect(_, _, _, _, ring, _),
   suspect(_, _, _, _, mac, _), listOfSuspects).

Any help would be appreciated.


回答1:


Here are some hints. You can state that Suspect1 has the same traits as Suspect2 just by Suspect1 = Suspect2, and to find out whether any member of a list Suspects has the traits of Suspect1 by member(Suspect1, Suspects).

The usual way of handling a disjunction in Prolog is by introducing a predicate with two clauses. E.g., a predicate that checks whether a suspect has either a ring or a mac is

ring_or_mac(suspect(_, _, _, _, ring, _)).
ring_or_mac(suspect(_, _, _, _, mac, _)).

If you put these hints together in the right way, you have a solution to your problem.



来源:https://stackoverflow.com/questions/16591868/or-procedure-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!