peroni_santo
peroni_santo

Reputation: 367

Add element to list depending on rule?

I understand it allows many facts to be written and a possibility tree created- is it possible to do this:

i would like to add value to list depending whether rule returns true or false?

could some person please show me some small code which can explain this?

thank

Upvotes: 0

Views: 94

Answers (1)

Yamaneko
Yamaneko

Reputation: 3563

Suppose you want to generate a list with all the mortals. It can be done with the following code:

% facts  
human(victor).
human(peroni).
human(hugo).

% rule
mortal(X) :-
        human(X).

% add X to a list only if rule mortal(X) is true.
add(X,L,[X|L]) :-
        mortal(X).

Example

? add( victor, [hugo], Result).
Result = [victor, hugo].

? add( peroni, [], Result).
Result = [peroni].

Upvotes: 2

Related Questions