如何根据Prolog中的事实创建列表? [英] How to create a list from facts in Prolog?
本文介绍了如何根据Prolog中的事实创建列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有这些事实:
man(john).
man(carl).
woman(mary).
woman(rose).
我需要创建谓词 people(List),它根据之前的事实返回一个包含每个男人和女人名字的列表.这是我需要的输出:
I need to create the predicate people(List), which returns a list with the name of every man and woman based on the previous facts. This is what I need as output:
?- people(X).
X = [john, carl, mary, rose]
这是我写的代码,但它不起作用:
And here is the code I wrote, but it's not working:
people(X) :- man(X) ; woman(X).
people(X|Tail) :- (man(X) ; woman(X)) , people(Tail).
有人可以帮忙吗?
推荐答案
使用 findall/3
:
people(L) :- findall(X, (man(X) ; woman(X)), L).
?- people(X).
X = [john, carl, mary, rose].
这篇关于如何根据Prolog中的事实创建列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文