Prolog 逻辑难题失败
Prolog Logic Puzzle Failure
我正在尝试解决在此处找到的逻辑难题:https://www.braingle.com/brainteasers/teaser.php?id=23826&op=2&comm=1#c
这些是线索:
The flowers were purchased in the following order: tulips, the flowers for the office, the purple flowers, the roses for the park, and the white flowers bought by Julia.
Bethany loves flowers but is allergic, so she would never have them indoors.
It rained on Wednesday and Friday, because of this, the wedding and birthday party had to be moved indoors.
Amy bought her flowers after Rachel, but before Kristen.
Rachel needed something more to add to her office, so she chose peach flowers to match her curtains.
On Wednesday the only purple flowers available at the flower shop were daisies.
The pink flowers were bought after the carnations, but before the lilies.
The flowers for the birthday were bought after the flowers for the office, but before the flowers for the wedding.
我已尝试使用以下代码在 prolog 中实现此功能:
/*
Use a nested predicate of flowers to store the solution
flowers(
f(Woman1,Flower1,Color1,Place1,Day1),
f(Woman2,Flower2,Color2,Place2,Day2),
f(Woman3,Flower3,Color3,Place3,Day3),
f(Woman4,Flower4,Color4,Place4,Day4),
f(Woman5,Flower5,Color5,Place5,Day5)
).
*/
/* The flowers predicate contains a given flower X */
has(X,F) :- F=flowers(X,_,_,_,_).
has(X,F) :- F=flowers(_,X,_,_,_).
has(X,F) :- F=flowers(_,_,X,_,_).
has(X,F) :- F=flowers(_,_,_,X,_).
has(X,F) :- F=flowers(_,_,_,_,X).
/*Defines that X occurs on an earlier day than Y */
before(X,Y,F) :- append(_,[X|Tail], F), append(_,[Y|_],Tail).
solve(F) :-
F = flowers(A,B,C,D,E),
/*clue 1 */
A = f(_,tulips,yellow,_,monday),
B = f(_,_,_,office,tuesday),
C = f(_,_,purple,_,wednesday),
D = f(_,roses,_,park,thursday),
E = f(julia,_,white,_,friday),
/* Clue 2 --> Look @ clue 3, Bethany can only have flowers in backyard or park */
has(f(bethany,_,_,backyard,_),F),
has(f(bethany,_,_,_,park,_),F),
/* Clue #4, Amy before Kristen, Rachel before Amy */
before(f(amy,_,_,_,_),f(kristen,_,_,_,_),F),
before(f(rachel,_,_,_,_),f(amy,_,_,_,_),F),
/* Clue #5 */
has(f(rachel,_,peach,office,_),F),
/* Clue #6 */
has(f(_,daisies,purple,_,wednesday),F),
/* Clue #7, pink before lillies, carnations before pink */
before(f(_,_,pink,_,_),f(_,lilies,_,_,_),F),
before(f(_,carnations,_,_,_),f(_,_,pink,_,_),F),
/* Clue #8 */
before(f(_,_,_,birthday,_),f(_,_,_,wedding,_),F),
before(f(_,_,_,office,_),f(_,_,_,birthday,_),F).
这似乎不起作用。我相信问题要么是我的代码在定义一朵花之前发生在另一朵花之前,但我不确定这是否是问题所在以及如何解决它。另外,我认为说 Bethany 为后院或公园买了花(不是婚礼、生日或办公室)是有问题的。
这是谜题的答案:
Julia: Lilies, White, Wedding, Friday
Amy: Daisies, Purple, Birthday, Wednesday
Bethany: Tulips, Yellow, Backyard, Monday
Rachel: Carnations, Peach, Office, Tuesday
Kristen: Roses, Pink, Park, Thursday
您 before/3
谓词试图将 flowers(A,B,C,D,E)
当作列表来处理。它总是失败。您需要将其定义为 [A,B,C,D,E]
才能正常工作。
如果将 flowers(A,B,C,D,E)
更改为 [A,B,C,D,E]
实际上更好,这样您就可以使用内置谓词 member/2
而不是 has/2
。
所以,首先我这样做了:
writeln(X) :- write(X), nl.
writelist([]).
writelist([H|T]) :- writeln(H), writelist(T).
/*Defines that X occurs on an earlier day than Y */
before(X,Y,F) :- append(F0, F1, F), member(X, F0), member(Y, F1).
你还想在后院和公园里说贝瑟尼。它必须是 or
关系。
这是我的 solve/1
版本:
solve(F) :-
/*clue 1 */
F = [
f(_,tulips,yellow,_,monday),
f(_,_,_,office,tuesday),
f(_,_,purple,_,wednesday),
f(_,roses,_,park,thursday),
f(julia,_,white,_,friday)
],
/* Clue 2 --> Look @ clue 3, Bethany can only have flowers in backyard or park */
(member(f(bethany,_,_,backyard,_),F);
member(f(bethany,_,_,_,park,_),F)),
/* Clue #4, Amy before Kristen, Rachel before Amy */
before(f(amy,_,_,_,_),f(kristen,_,_,_,_),F),
before(f(rachel,_,_,_,_),f(amy,_,_,_,_),F),
/* Clue #5 */
member(f(rachel,_,peach,office,_),F),
/* Clue #6 */
member(f(_,daisies,purple,_,wednesday),F),
/* Clue #7, pink before lillies, carnations before pink */
before(f(_,_,pink,_,_),f(_,lilies,_,_,_),F),
before(f(_,carnations,_,_,_),f(_,_,pink,_,_),F),
/* Clue #8 */
before(f(_,_,_,birthday,_),f(_,_,_,wedding,_),F),
before(f(_,_,_,office,_),f(_,_,_,birthday,_),F),
true.
现在,当我查询 ?- solve(F), writelist(F).
时,我得到了这个:
f(bethany, tulips, yellow, backyard, monday)
f(rachel, carnations, peach, office, tuesday)
f(amy, daisies, purple, birthday, wednesday)
f(kristen, roses, pink, park, thursday)
f(julia, lilies, white, wedding, friday)
Yes.
我正在尝试解决在此处找到的逻辑难题:https://www.braingle.com/brainteasers/teaser.php?id=23826&op=2&comm=1#c
这些是线索:
The flowers were purchased in the following order: tulips, the flowers for the office, the purple flowers, the roses for the park, and the white flowers bought by Julia.
Bethany loves flowers but is allergic, so she would never have them indoors.
It rained on Wednesday and Friday, because of this, the wedding and birthday party had to be moved indoors.
Amy bought her flowers after Rachel, but before Kristen.
Rachel needed something more to add to her office, so she chose peach flowers to match her curtains.
On Wednesday the only purple flowers available at the flower shop were daisies.
The pink flowers were bought after the carnations, but before the lilies.
The flowers for the birthday were bought after the flowers for the office, but before the flowers for the wedding.
我已尝试使用以下代码在 prolog 中实现此功能:
/*
Use a nested predicate of flowers to store the solution
flowers(
f(Woman1,Flower1,Color1,Place1,Day1),
f(Woman2,Flower2,Color2,Place2,Day2),
f(Woman3,Flower3,Color3,Place3,Day3),
f(Woman4,Flower4,Color4,Place4,Day4),
f(Woman5,Flower5,Color5,Place5,Day5)
).
*/
/* The flowers predicate contains a given flower X */
has(X,F) :- F=flowers(X,_,_,_,_).
has(X,F) :- F=flowers(_,X,_,_,_).
has(X,F) :- F=flowers(_,_,X,_,_).
has(X,F) :- F=flowers(_,_,_,X,_).
has(X,F) :- F=flowers(_,_,_,_,X).
/*Defines that X occurs on an earlier day than Y */
before(X,Y,F) :- append(_,[X|Tail], F), append(_,[Y|_],Tail).
solve(F) :-
F = flowers(A,B,C,D,E),
/*clue 1 */
A = f(_,tulips,yellow,_,monday),
B = f(_,_,_,office,tuesday),
C = f(_,_,purple,_,wednesday),
D = f(_,roses,_,park,thursday),
E = f(julia,_,white,_,friday),
/* Clue 2 --> Look @ clue 3, Bethany can only have flowers in backyard or park */
has(f(bethany,_,_,backyard,_),F),
has(f(bethany,_,_,_,park,_),F),
/* Clue #4, Amy before Kristen, Rachel before Amy */
before(f(amy,_,_,_,_),f(kristen,_,_,_,_),F),
before(f(rachel,_,_,_,_),f(amy,_,_,_,_),F),
/* Clue #5 */
has(f(rachel,_,peach,office,_),F),
/* Clue #6 */
has(f(_,daisies,purple,_,wednesday),F),
/* Clue #7, pink before lillies, carnations before pink */
before(f(_,_,pink,_,_),f(_,lilies,_,_,_),F),
before(f(_,carnations,_,_,_),f(_,_,pink,_,_),F),
/* Clue #8 */
before(f(_,_,_,birthday,_),f(_,_,_,wedding,_),F),
before(f(_,_,_,office,_),f(_,_,_,birthday,_),F).
这似乎不起作用。我相信问题要么是我的代码在定义一朵花之前发生在另一朵花之前,但我不确定这是否是问题所在以及如何解决它。另外,我认为说 Bethany 为后院或公园买了花(不是婚礼、生日或办公室)是有问题的。
这是谜题的答案:
Julia: Lilies, White, Wedding, Friday
Amy: Daisies, Purple, Birthday, Wednesday
Bethany: Tulips, Yellow, Backyard, Monday
Rachel: Carnations, Peach, Office, Tuesday
Kristen: Roses, Pink, Park, Thursday
您 before/3
谓词试图将 flowers(A,B,C,D,E)
当作列表来处理。它总是失败。您需要将其定义为 [A,B,C,D,E]
才能正常工作。
如果将 flowers(A,B,C,D,E)
更改为 [A,B,C,D,E]
实际上更好,这样您就可以使用内置谓词 member/2
而不是 has/2
。
所以,首先我这样做了:
writeln(X) :- write(X), nl.
writelist([]).
writelist([H|T]) :- writeln(H), writelist(T).
/*Defines that X occurs on an earlier day than Y */
before(X,Y,F) :- append(F0, F1, F), member(X, F0), member(Y, F1).
你还想在后院和公园里说贝瑟尼。它必须是 or
关系。
这是我的 solve/1
版本:
solve(F) :-
/*clue 1 */
F = [
f(_,tulips,yellow,_,monday),
f(_,_,_,office,tuesday),
f(_,_,purple,_,wednesday),
f(_,roses,_,park,thursday),
f(julia,_,white,_,friday)
],
/* Clue 2 --> Look @ clue 3, Bethany can only have flowers in backyard or park */
(member(f(bethany,_,_,backyard,_),F);
member(f(bethany,_,_,_,park,_),F)),
/* Clue #4, Amy before Kristen, Rachel before Amy */
before(f(amy,_,_,_,_),f(kristen,_,_,_,_),F),
before(f(rachel,_,_,_,_),f(amy,_,_,_,_),F),
/* Clue #5 */
member(f(rachel,_,peach,office,_),F),
/* Clue #6 */
member(f(_,daisies,purple,_,wednesday),F),
/* Clue #7, pink before lillies, carnations before pink */
before(f(_,_,pink,_,_),f(_,lilies,_,_,_),F),
before(f(_,carnations,_,_,_),f(_,_,pink,_,_),F),
/* Clue #8 */
before(f(_,_,_,birthday,_),f(_,_,_,wedding,_),F),
before(f(_,_,_,office,_),f(_,_,_,birthday,_),F),
true.
现在,当我查询 ?- solve(F), writelist(F).
时,我得到了这个:
f(bethany, tulips, yellow, backyard, monday) f(rachel, carnations, peach, office, tuesday) f(amy, daisies, purple, birthday, wednesday) f(kristen, roses, pink, park, thursday) f(julia, lilies, white, wedding, friday) Yes.