Stuck on a Prolog problem. I know the answer (because I did it on paper first), but I cannot figure out how to get Prolog to come up with the answer.
Problem:
Bill eats a snack every night, having a different fruit and different nuts each night. From the statements below, identify what Bill had for a snack for each weeknight last week.
a) The apple was eaten later in the week than the mango.
b) The banana was eaten later in the week than both the almonds and peanuts, but earlier in the week than the pear.
c) The cashews were eaten earlier in the week than both the banana and the apricot, but later in the week than the peanuts.
d) The pecans were not eaten the evening after the almonds.
e) Bill ate walnuts one night.
Note that the problem is about 5 weeknights (Monday through Friday), and mentions 5 kinds of fruit and 5 kinds of nuts. Your program should solve the problem and print out the solution, which will be a set of 5 triples like (Monday, apple, pecans), ... (Friday, mango, walnuts).
Clearly, these are not the correct answers, but just values to show you what the solution will look like.
Code so far:
before_in_week(X, Y, Days) :-
nth1(Xi, Days, X),
nth1(Yi, Days, Y),
Xi < Yi.
print_solve([Head|Tail]) :-
write(Head),
nl,
print_solve(Tail).
solve(A) :-
% all triples
A = [[day1, fruit1, nut1],
[day2, fruit2, nut2],
[day3, fruit3, nut3],
[day4, fruit4, nut4],
[day5, fruit5, nut5]],
Days = [monday, tuesday, wednesday, thursday, friday],
Days = [day1, day2, day3, day4, day5],
Fruits = [apple,banana,pear,mango,apricot],
permutation(Fruits, [fruit1, fruit2, fruit3, fruit4, fruit5]),
Nuts = [almonds,pecans,cashews,peanuts,walnuts],
permutation(Nuts, [nut1, nut2, nut3, nut4, nut5]),
% clue 1 - mango before apple
fruit5 \= mango,
member([C1,mango,_], A),
member([C2,apple,_], A), before_in_week(C1,C2,Days),
% clue 2 - banana after almonds and peanuts, but before pear
fruit5 \= banana,
member([C1,banana,_], A),
member([C2,pear,_], A), before_in_week(C1,C2,Days),
member([C3,_,almonds], A), before_in_week(C3,C1,Days),
member([C4,_,peanuts], A), before_in_week(C4,C1,Days),
% clue 3 - cashews before banana and apricot, but after peanuts
nut5 \= peanuts,
member([C1,_,cashews], A),
member([C2,_,peanuts], A), before_in_week(C1,C2,Days),
member([C3,banana,_], A), before_in_week(C3,C1,Days),
member([C4,apricot,_], A), before_in_week(C4,C1,Days),
% clue 4 - pecans not night after almonds
nut5 \= almonds,
% clue 5 - ate walnuts one night
print_solve(A).
fruit5 \= mango
is always going to be true. both atoms are different. Prolog's variables must start with an Upper Case Letter, likeFruit5
. for the "ununifiable" goal (Fruit5 \= mango
) to succeed, the logvar (Fruit5
) should be already instantiated by the time the goal is tried (appears in the overall predicate), otherwise that goal will fail because a free logvar is always unifiable with a given value by being instantiated with it. – Will NessI did it on paper first
You probably have no idea how much that puts you ahead as someone with an ability to problem solve. – Guy Coder