Pedro Silva
Pedro Silva

Reputation: 1

Is it possible to have a list as a fact in prolog?

I am new to prolog an wanted to know if it is possible to have a list as a fact so i can use it with if, and, then. For example:

list(a,b,c,d).
fact(x).
fact(y).

if x and y then list(a,b,c,d).

Upvotes: 0

Views: 64

Answers (1)

David Tonhofer
David Tonhofer

Reputation: 15338

Sure you can, but what you write makes no sense. A "Prolog program" is a list of implications X <= A & ... & B which determines which of your queries come back with false or a true (a constructive true as you get the values for variables making the query true as answer). So the idea of having a if x and y then list(a,b,c,d) does not really fit into this.. unless you want to say something like

foo([a,b,c,d]).   % This is true! List [a,b,c,d] has attribute "foo"
bar(a).           % This is true! The atom a has attribute "bar"
baz(b).           % This is true! The atom b has attribute "baz"

% A value for X has attribute "solution" if:
% 1) It has attribute bar
% 2) It is a member of any list that has attribute "foo"

solution(X) :- bar(X),foo(List),member(X,List).
?- solution(X).

will give X=a.

Upvotes: 1

Related Questions