我被要求使用 Prolog 解决类似于斑马谜题的谜题。相反,我试图找出吃馅饼的人的国籍。我知道这个问题有不同的解决方案,但是我试图通过使用约束和排列来解决这个难题。但是,运行 pie_eater 谓词会返回 false,并且在跟踪代码时它不会停止运行。
约束列表如下:
1) the Irish person lives next to the yellow house
2) the person who likes donuts lives next to the person who enjoys embroidery
3) the person who enjoys trainspotting lives next to the one who likes pizza
4) the person who enjoys pachinko likes beets
5) the Czech lives in the purple house
6) the person living in the center house listens to grunge
7) the person who enjoys glassblowing listens to hip hop
8) the American likes tofu
9) the Dutch person listens to J-Pop
10) the pink house's owner listens to country
11) the owner of the green house enjoys embroidery
12) the Irish person lives in the first house
13) the pink house is on the left of the orange house
14) the Japanese person enjoys gardening
15) the person who enjoys trainspotting has a neighbor who listens to jazz
在我的代码中,我定义了一个包含 5 个房屋的列表Street 。然后,我为列表中每个元素的每个对应形容词定义变量,以用于我的排列猜测。我将每个约束都翻译成代码。为了生成答案,我为每所房子的每组变量创建了排列。在底部,我定义了谓词leftof和nextto以满足问题的一些约束。
代码如下:
%who eats pie
pie_eater(PieEater) :-
puzzle(X),
member(house(PieEater, _, _, _, pie), X).
puzzle(Street) :-
Street = [H1, H2, H3, H4, H5], %street of 5 houses
%vars for permutation
H1 = house(Nat1, Col1, Mus1, Hob1, Food1),
H2 = house(Nat2, Col2, Mus2, Hob2, Food2),
H3 = house(Nat3, Col3, Mus3, Hob3, Food3),
H4 = house(Nat4, Col4, Mus4, Hob4, Food4),
H5 = house(Nat5, Col5, Mus5, Hob5, Food5),
% facts
nextto(house(irish, _, _, _, _), house(_, yellow, _, _, _), Street), %irish next to yellow house
nextto(house(_, _, _, _, donuts), house(_, _, _, embroidery, _), Street), %donut next to embrodier lover
nextto(house(_, _, _, trainspotting, _), house(_, _, _, _, pizza), Street), %trainspotting next to pizza ouse
member(house(_, _, _, pachinko, beets), Street), %pachinko person eats beets
member(house(czech, purple, _, _, _), Street), %czech person lives in purple house
[_, _, house(_, _, grunge, _, _), _, _] = Street, %middle house listens to grunge
member(house(_, _, hiphop, glassblowing, _), Street), %hiphop lover likes glassblowing
member(house(american, _, _, _, tofu), Street), %american likes tofu
member(house(dutch, _, jpop, _, _), Street), %dutch person likes jpop
member(house(_, pink, country, _, _), Street), %pink house person likes country music
member(house(_, green, _, embroidery, _), Street), %green house person likes embroidery
[house(irish, _, _, _, _), _, _, _, _] = Street, %irish person in first house
leftof(house(_, pink, _, _, _),house(_, orange, _, _, _), Street), %pink house left of orange house
member(house(japanese, _, _, gardening, _), Street), %japanese person likes gardening
member(house(_, _, jazz, trainspotting, _), Street), %trainspotting person likes jazz
member(house(_, _, _, _, pie), Street), %one person likes pie
%permutation guesses
permutation([irish, czech, american, dutch, japanese],[Nat1, Nat2, Nat3, Nat4, Nat5]), %nat permutations
permutation([yellow, purple, pink, green, orange],[Col1, Col2, Col3, Col4, Col5]), %color permutations
permutation([grunge, hiphop, jpop, country, jazz],[Mus1, Mus2, Mus3, Mus4, Mus5]), %music permutation
permutation([trainspotting, pachinko, glassblowing, embroidery, gardening],[Hob1, Hob2, Hob3, Hob4, Hob5]), %hobby permutation
permutation([donuts, beets, pizza, tofu, pie],[Food1, Food2, Food3, Food4, Food5]). %food permutation
%defining other predicates to help solve puzzle
nextto(X, Y, List) :- leftof(X, Y, List).
nextto(X, Y, List) :- leftof(Y, X, List).
%check if left of
leftof(L, R, [L,R|_]).
leftof(L, R, [_ | Rest]) :- leftof(L, R, Rest).
盯着我的代码几个小时,我觉得它应该可以工作,但是我仍然无法弄清楚为什么我不能生成正确的排列,因为我已经定义了所有的约束。在这一点上,我被困住了。
自己做题,我相信日本人就是答案。