shadow
shadow

Reputation: 27

Querying each element of a list

I am doing homework for AI class and I am writing a prolog program.

I am supposed to take a list of names and check if each person in the list belongs to a specific country chosen.

what i have so far

% facts
person(bruce, australia, rhodri, bronwyn).
person(rhodri, newyork, dan, mary).
person(bronwyn, miami, gar, roo).
person(dan, miami, george, mimi).
person(mary, texas, mack, tiki).
person(gar, jamaica, zid, rem).
person(roo, newzealand, john, jill).

person(tom, mayday, dick, mel).
person(dick, newyork, harry, rin).
person(mel, miami, tom, stacey).
person(harry, miami, george, mimi).
person(rin, texas, mack, tiki).
person(tom, jamaica, zid, rem).
person(stacey, newzealand, john, jill).

% rules

eligible(P,C) :-
   person(P, C, F, M) , !
 ; person(F, C, Newfather, Newmother), !
 ; person(M, C, Newfather, Newmother), !
 ; person(Newfather, C, Grandfather , Grandmother), !
 ; person(Newmother, C, Grandfather, Grandmother).

checkteam([] , C). 
checkteam([H|T] , C) :- eligible(H, C) , checkteam(T, C).

the last two lines in particular i am having issues with, i am trying to test each member of the list with the eligible() function then let the first element of tail become the head and repeat.

I cant figure out a way to test each member and then display a fail if any of the members are not eligible or true if all members belong to that country.

Thanks in advance.

EDIT: was fooling around and changed the code a little, as for results

?- checkteam([bruce, dan], mayday).
true.

even though neither bruce or dan are from mayday or any parents or grandparents that do.

Upvotes: 1

Views: 90

Answers (1)

user1812457
user1812457

Reputation:

Your eligible predicate doesn't make sense to me (probably I am misunderstanding). But, if person is defined as person(Name, Country, Father, Mother) then it could be:

eligible(Name, Country) :- person(Name, Country, _, _).
eligible(Name, Country) :- person(Name, _, Father, _),
                           person(Father, Country, _, _).
eligible(Name, Country) :- person(Name, _, _, Mother),
                           person(Mother, Country, _, _).

Then your checkteam should still give you a warning. Put an underscore at the beginning of the variable name to get rid of it:

checkteam([], _Country).

Upvotes: 1

Related Questions