How to create a list from facts in Prolog?

五迷三道 提交于 2020-01-03 13:32:06

问题


There are these facts:

man(john).
man(carl).
woman(mary).
woman(rose).

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).

Could someone please help?


回答1:


Using findall/3:

people(L) :- findall(X, (man(X) ; woman(X)), L).
?- people(X).
X = [john, carl, mary, rose].



回答2:


Here we go:

person(anne).
person(nick).

add(B, L):-
    person(P),
    not(member(P, B)),
    add([P|B], L),!.

add(B, L):-
    L = B,!.

persons(L):-
    add([], L).


来源:https://stackoverflow.com/questions/19615150/how-to-create-a-list-from-facts-in-prolog

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