Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

list - "Or" procedure in prolog

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...