检查序言列表元素的关系 [英] Checking a relation of a prolog list element
本文介绍了检查序言列表元素的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我说有一段恋情
Happy(james)
Happy(harry)
unhappy(Tom)
unhappy(Ben)
unhappy(Dick)
然后是人员列表
[Ben, James, Harry, Tom, Dick]
如何迭代列表并检查每个列表元素的布尔值是否满意?
How can I iterate the list and check the boolean of each list element as to whether they are happy or not?
推荐答案
首先,在Prolog中,如果单词以大写字母开头,则表示它是一个变量.因此,您应该注意这一点.
Well, first of all, in Prolog, if a word starts with a capital letter, it means that it is a variable. So you should be careful with that.
这是更正后的我的数据库:
This is my database after the correction:
happy(james).
happy(harry).
unhappy(tom).
unhappy(ben).
unhappy(dick).
并且我添加了一个递归规则,该规则可以帮助我查看谁在给定列表中感到高兴和不高兴:
and I added a recursive rule that helps me see who is happy and who is not from a given list:
emotion([]).
emotion([H|T]):- happy(H),emotion(T),
write(H),write(' is happy.'),
nl;
unhappy(H),emotion(T),
write(H),write(' is unhappy.'),
nl.
这是结果:
4 ?- emotion([ben, james, harry, tom, dick]).
dick is unhappy.
tom is unhappy.
harry is happy.
james is happy.
ben is unhappy.
true.
这篇关于检查序言列表元素的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文