Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list from facts in Prolog?

Tags:

prolog

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?

like image 785
renatov Avatar asked Oct 27 '13 06:10

renatov


1 Answers

Using findall/3:

people(L) :- findall(X, (man(X) ; woman(X)), L).
?- people(X).
X = [john, carl, mary, rose].
like image 75
ДМИТРИЙ МАЛИКОВ Avatar answered Sep 28 '22 04:09

ДМИТРИЙ МАЛИКОВ