I am sure this will be pretty easy, but I have spent hours trying to figure this out.. I am running Visual Prolog 5.2:
The problem is:
Consider four men with last names of Baker, Carpenter, Miller, and Farmer. Assume that the four professions represented by this group include a baker, a carpenter, a miller, and a farmer. Assume further that each person has a profession that does not correspond to their last name.
Each of these four men has a son. The four sons have professions of baker, carpenter, miller, and farmer. Assume again that each person has a profession that does not correspond to their last name.
Assume that we also know the following facts:
No son has the same profession as his father.
Baker has the same profession as the carpenter's son.
The farmer's son is a baker.
What I have so far:
DOMAINS
father = father(lastname,profession)
son = son(lastname2,profession2)
list2 = son*
list = father*
lastname = string
profession = string
lastname2 = string
profession2 = string
PREDICATES
nondeterm member(father, list)
nondeterm member(son,list2)
solution
CLAUSES
% Predicate to check for membership in a list
member(Item, [Item|_]).
member(Item,[_|Tail]) :- member(Item,Tail).
solution:-
% Form of list of names and professions
List = [father("Baker" , BakerFatherJob),
father("Carpenter" , CarpenterFatherJob),
father("Miller" , MillerFatherJob),
father("Farmer",FarmerFatherJob)],
List2 = [son("Baker" , BakerSonJob),
son("Carpenter" , CarpenterSonJob),
son("Miller" , MillerSonJob),
son("Farmer",FarmerSonJob)],
% One person must be assigned to each profession
member(father(_, "Baker"), List),
member(father(_, "Carpenter"), List),
member(father(_, "Miller"), List),
member(father(_, "Farmer"), List),
member(son(_, "Baker"), List2),
member(son(_, "Carpenter"), List2),
member(son(_, "Miller"), List2),
member(son(_, "Farmer"), List2),
% Rules that were found in the problem statement
BakerFatherJob<>BakerSonJob,
CarpenterFatherJob<>CarpenterSonJob,
MillerFatherJob<>MillerSonJob,
FarmerFatherJob<>FarmerSonJob,
BakerFatherJob <> "Baker",
CarpenterFatherJob <> "Carpenter",
MillerFatherJob <> "Miller",
FarmerFatherJob <> "Farmer",
BakerSonJob <> "Baker",
CarpenterSonJob <> "Carpenter",
MillerSonJob <> "Miller",
FarmerSonJob <> "Farmer",
% Display this solution
write("Father Baker has job ", BakerFatherJob),nl,
write("Father Carpenter has job ", CarpenterFatherJob), nl,
write("Father Miller has job ", MillerFatherJob),nl,
write("Father Farmer has job ", FarmerFatherJob),nl,
write(" "), nl,
write("Son Baker has job ", BakerSonJob),nl,
write("Son Carpenter has job ", CarpenterSonJob), nl,
write("Son Miller has job ", MillerSonJob),nl,
write("Son Farmer has job ", FarmerSonJob),nl,
write(" "), nl,
% Continue looking for more solutions
fail.
solution:-
write("=====> ALL SOLUTIONS HAVE BEEN FOUND <=====").
GOAL
solution.
The part I cannot figure out how to implement is:
Baker has the same profession as the carpenter's son.
The farmer's son is a baker.
I am not all too familiar with Prolog, and this may seem easy, but any help or input would be appreciated
The problem is:
Consider four men with last names of Baker, Carpenter, Miller, and Farmer. Assume that the four professions represented by this group include a baker, a carpenter, a miller, and a farmer. Assume further that each person has a profession that does not correspond to their last name.
Each of these four men has a son. The four sons have professions of baker, carpenter, miller, and farmer. Assume again that each person has a profession that does not correspond to their last name.
Assume that we also know the following facts:
No son has the same profession as his father.
Baker has the same profession as the carpenter's son.
The farmer's son is a baker.
What I have so far:
DOMAINS
father = father(lastname,profession)
son = son(lastname2,profession2)
list2 = son*
list = father*
lastname = string
profession = string
lastname2 = string
profession2 = string
PREDICATES
nondeterm member(father, list)
nondeterm member(son,list2)
solution
CLAUSES
% Predicate to check for membership in a list
member(Item, [Item|_]).
member(Item,[_|Tail]) :- member(Item,Tail).
solution:-
% Form of list of names and professions
List = [father("Baker" , BakerFatherJob),
father("Carpenter" , CarpenterFatherJob),
father("Miller" , MillerFatherJob),
father("Farmer",FarmerFatherJob)],
List2 = [son("Baker" , BakerSonJob),
son("Carpenter" , CarpenterSonJob),
son("Miller" , MillerSonJob),
son("Farmer",FarmerSonJob)],
% One person must be assigned to each profession
member(father(_, "Baker"), List),
member(father(_, "Carpenter"), List),
member(father(_, "Miller"), List),
member(father(_, "Farmer"), List),
member(son(_, "Baker"), List2),
member(son(_, "Carpenter"), List2),
member(son(_, "Miller"), List2),
member(son(_, "Farmer"), List2),
% Rules that were found in the problem statement
BakerFatherJob<>BakerSonJob,
CarpenterFatherJob<>CarpenterSonJob,
MillerFatherJob<>MillerSonJob,
FarmerFatherJob<>FarmerSonJob,
BakerFatherJob <> "Baker",
CarpenterFatherJob <> "Carpenter",
MillerFatherJob <> "Miller",
FarmerFatherJob <> "Farmer",
BakerSonJob <> "Baker",
CarpenterSonJob <> "Carpenter",
MillerSonJob <> "Miller",
FarmerSonJob <> "Farmer",
% Display this solution
write("Father Baker has job ", BakerFatherJob),nl,
write("Father Carpenter has job ", CarpenterFatherJob), nl,
write("Father Miller has job ", MillerFatherJob),nl,
write("Father Farmer has job ", FarmerFatherJob),nl,
write(" "), nl,
write("Son Baker has job ", BakerSonJob),nl,
write("Son Carpenter has job ", CarpenterSonJob), nl,
write("Son Miller has job ", MillerSonJob),nl,
write("Son Farmer has job ", FarmerSonJob),nl,
write(" "), nl,
% Continue looking for more solutions
fail.
solution:-
write("=====> ALL SOLUTIONS HAVE BEEN FOUND <=====").
GOAL
solution.
The part I cannot figure out how to implement is:
Baker has the same profession as the carpenter's son.
The farmer's son is a baker.
I am not all too familiar with Prolog, and this may seem easy, but any help or input would be appreciated