renatov
renatov

Reputation: 5095

How to create a list from facts in 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?

Upvotes: 7

Views: 19768

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

Upvotes: 2

Using findall/3:

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

Upvotes: 20

Related Questions