Lets say I have a relations
Happy(james)
Happy(harry)
unhappy(Tom)
unhappy(Ben)
unhappy(Dick)
And then a list of people
[Ben, James, Harry, Tom, Dick]
How can I iterate the list and check the boolean of each list element as to whether they are happy or not?
Best How To :
Well, first of all, in Prolog, if a word starts with a capital letter, it means that it is a variable. So you should be careful with that.
This is my database after the correction:
happy(james).
happy(harry).
unhappy(tom).
unhappy(ben).
unhappy(dick).
and I added a recursive rule that helps me see who is happy and who is not from a given list:
emotion([]).
emotion([H|T]):- happy(H),emotion(T),
write(H),write(' is happy.'),
nl;
unhappy(H),emotion(T),
write(H),write(' is unhappy.'),
nl.
Here is the result:
4 ?- emotion([ben, james, harry, tom, dick]).
dick is unhappy.
tom is unhappy.
harry is happy.
james is happy.
ben is unhappy.
true.