2016-04-20 42 views
2

我在Prolog中實現了愛因斯坦謎語,我試圖找出誰在家裏有一條魚。在第一家愛因斯坦謎語與術語列表

  1. 挪威人住
  2. 英國人住在紅房子:)

    規則;
    我不能在這個代碼查找故障並跟蹤選項沒有這個問題幫助。

  3. 溫室坐落在白宮左側。
  4. 丹麥人喝茶。
  5. 輕煙民住貓的飼養員旁邊。
  6. 黃色房屋的居民抽雪茄。
  7. 德國人抽水管。
  8. 中心房屋的居民喝牛奶。
  9. 輕煙的人有鄰居喝水。
  10. 無需過濾器繁殖鳥類的菸捲煙。
  11. 瑞典人養殖的狗。
  12. 挪威人住在藍屋旁邊。
  13. 馬的飼養員住在黃房子旁邊。
  14. 煙薄荷醇喝啤酒。
  15. 在溫室裏,他們喝咖啡。

這裏是我的代碼:

on_the_left(X, Y, N) :- 
    Y is X - 1, 
    \+ Y < 1, 
    \+ X > N. 

next_to(X, Y, N) :- 
    (Y is X + 1; 
     Y is X - 1), 
    \+ X > N, 
    \+ Y > N, 
    \+ X < 1, 
    \+ Y < 1. 

fish(Who) :- 
    Houses = [ 
     house(1, _Color1, _From1, _Animal1, _Drink1, _Smoke1), 
     house(2, _Color2, _From2, _Animal2, _Drink2, _Smoke2), 
     house(3, _Color3, _From3, _Animal3, _Drink3, _Smoke3), 
     house(4, _Color4, _From4, _Animal4, _Drink4, _Smoke4), 
     house(5, _Color5, _From5, _Animal5, _Drink5, _Smoke5) ], 
    N is 5, 
    %-- hint 1 
    member(house(1, _, norway, _, _, _), Houses), 
    %-- hint 2 
    member(house(_, red, england, _, _, _), Houses), 
    %-- hint 3 - on_the_left 
    member(house(GREEN, green, _, _, _, _), Houses), 
    member(house(WHITE, white, _, _, _, _), Houses), 
    on_the_left(GREEN, WHITE, N), 
    %-- hint 4 
    member(house(_, _, denmark, _, tea, _), Houses), 
    %-- hint 5 - next_to 
    member(house(LIGHT, _, _, _, _, light), Houses), 
    member(house(CAT, _, _, cat, _, light), Houses), 
    next_to(LIGHT, CAT, N), 
    %-- hint 6 
    member(house(_, yellow, _, _, _, cigar), Houses), 
    %-- hint 7 
    member(house(_, _, germany, _, _, waterpipe), Houses), 
    %-- hint 8 
    member(house(3, _, _, _, milk, _), Houses), 
    %-- hint 9 - next_to 
    member(house(WATER, _, _, _, water, _), Houses), 
    next_to(LIGHT, WATER, N), 
    %-- hint 10 
    member(house(_, _, _, bird, _, nofilter), Houses), 
    %-- hint 11 
    member(house(_, _, sweden, dog, _, _), Houses), 
    %-- hint 12 - next_to 
    member(house(NORWAY, _, norway, _, _, _), Houses), 
    member(house(BLUE, blue, _, _, _, _), Houses), 
    next_to(NORWAY, BLUE, N), 
    %-- hint 13 - next_to 
    member(house(HORSE, _, _, horse, _, _), Houses), 
    next_to(HORSE, GREEN, N), 
    %-- hint 14 
    member(house(_, _, _, _, beer, menthol), Houses), 
    %-- hint 15 
    member(house(_, green, _, _, coffee, _), Houses), 

    %-- FINAL QUESTION - WHO LET THE FISH OUT? 
    member(house(_, _, _, fish, _, _), Houses), 
    member(house(_, _, Who, fish, _, _), Houses). 

我嘗試了很多組合,但:

- 魚(誰?)。
錯誤。

編輯:現在
代碼工作,我改變了:

1 *來源:

%-- hint 5 - next_to 
member(house(LIGHT, _, _, _, _, light), Houses), 
member(house(CAT, _, _, cat, _, light), Houses), 

要:

%-- hint 5 - next_to 
    member(house(LIGHT, _, _, _, _, light), Houses), 
    member(house(CAT, _, _, cat, _, _), Houses), 

2 *來源:

%-- hint 13 - next_to 
member(house(HORSE, _, _, horse, _, _), Houses), 
next_to(HORSE, GREEN, N), 

要:

%-- hint 13 - next_to 
member(house(YELLOW, yellow, _, _, _, _), Houses), 
member(house(HORSE, _, _, horse, _, _), Houses), 
next_to(HORSE, YELLOW, N), 

如果你正在讀這一下關於幫手結構@Enigmativity評論藏漢謂詞。

+0

不錯,謝謝。我添加了一個規則。 5提示中的錯誤被修復,但程序仍然無法正常工作。 – CryptoNewbie

回答

3

你的線索有兩個錯誤 - 第一個已經用light吸菸者修復。第二個是horse店主住在yellow房子旁邊,而不是​​。

現在,我的序言ch嚥了\+運算符,所以我重新編寫了你的​​助手謂詞。這是我做過什麼:

 
fish(Who) :- 
    Houses = [ 
     house(_Color1, _From1, _Animal1, _Drink1, _Smoke1), 
     house(_Color2, _From2, _Animal2, _Drink2, _Smoke2), 
     house(_Color3, _From3, _Animal3, _Drink3, _Smoke3), 
     house(_Color4, _From4, _Animal4, _Drink4, _Smoke4), 
     house(_Color5, _From5, _Animal5, _Drink5, _Smoke5) ], 
    first(house(_, norway, _, _, _), Houses), %-- hint 1 
    member(house(red, england, _, _, _), Houses), %-- hint 2 
    on_the_left(house(green, _, _, _, _), house(white, _, _, _, _), Houses), %-- hint 3 - on_the_left 
    member(house(_, denmark, _, tea, _), Houses), %-- hint 4 
    next_to(house(_, _, _, _, light), house(_, _, cat, _, _), Houses), %-- hint 5 - next_to 
    member(house(yellow, _, _, _, cigar), Houses), %-- hint 6 
    member(house(_, germany, _, _, waterpipe), Houses), %-- hint 7 
    middle(house(_, _, _, milk, _), Houses), %-- hint 8 
    next_to(house(_, _, _, _, light), house(_, _, _, water, _), Houses), %-- hint 9 - next_to 
    member(house(_, _, bird, _, nofilter), Houses), %-- hint 10 
    member(house(_, sweden, dog, _, _), Houses), %-- hint 11 
    next_to(house(_, norway, _, _, _), house(blue, _, _, _, _), Houses), %-- hint 12 - next_to 
    next_to(house(_, _, horse, _, _), house(yellow, _, _, _, _), Houses), %-- hint 13 - next_to 
    member(house(_, _, _, beer, menthol), Houses), %-- hint 14 
    member(house(green, _, _, coffee, _), Houses), %-- hint 15 
    member(house(_, Who, fish, _, _), Houses), 
    write(Houses), nl. 

我:

 
[house(yellow, norway, cat, water, cigar), house(blue, denmark, horse, tea, light), house(red, england, bird, milk, nofilter), house(green, germany, fish, coffee, waterpipe), house(white, sweden, dog, beer, menthol)] 
germany 
+0

謝謝,我修復了第二個錯誤,現在它的工作很好。我需要睡眠;)它與我的版本的助手謂詞一起工作,但更好的做法是像你一樣寫它,對吧? – CryptoNewbie

+0

@CryptoNewbie - 我儘量避免數字操作。 Prolog最擅長的是結構解決方案 - 所以像'middle(X,[_,_,X,_,_])'這樣的謂詞是簡單和結構化的。我也嘗試在'fish/1'謂詞中將每個線索都作爲一個單詞來實現 - 這使得它更容易閱讀,因此更易於調試和維護。這將是我對你的兩個建議。 – Enigmativity

+1

哪個Prolog在'(\ +)/ 1'上扼制? – false

1

只是爲了顯示一種替代編碼方案:

 
first(H,[H|_]). 

on_the_left(X,Y,[X,Y|_]). 
on_the_left(X,Y,[_|Hs]) :- on_the_left(X,Y,Hs). 

next_to(X,Y,[X,Y|_]). 
next_to(X,Y,[Y,X|_]). 
next_to(X,Y,[_|Hs]) :- next_to(X,Y,Hs). 

middle(X,[_,_,X,_,_]). 

現在拼圖這些線索很好地工作

solve :- solve(Sol, From), writeln(From), maplist(writeln, Sol). 

solve(Sol, From) :- 
    phrase((from(1, norway) 
     ,color(red) = from(england) 
     ,color(GREEN, green), color(WHITE, white), {GREEN is WHITE-1} 
     ,from(denmark) = drink(tea) 
     ,smoke(Light, light), animal(Cat, cat), next_to(Light, Cat) 
     ,color(Yellow, yellow), smoke(Yellow, cigar) 
     ,from(germany) = smoke(waterpipe) 
     ,drink(3, milk) 
     ,drink(Water, water), next_to(Light, Water) 
     ,animal(bird) = smoke(nofilter) 
     ,from(sweden) = animal(dog) 
     ,from(NORWAY, norway), color(BLUE, blue), next_to(NORWAY, BLUE) 
     ,animal(HORSE, horse), next_to(HORSE, GREEN) % next_to(HORSE, Yellow) 
     ,drink(beer) = smoke(menthol) 
     ,color(green) = drink(coffee) 
     ,animal(Fish, fish), from(Fish, From) 
    ), [[1,_,_,_,_,_], 
     [2,_,_,_,_,_], 
     [3,_,_,_,_,_], 
     [4,_,_,_,_,_], 
     [5,_,_,_,_,_] 
    ], Sol). 

state(S), [A,B,C,D,E] --> [A,B,C,D,E], {member(S, [A,B,C,D,E])}. 

color(A, B) --> state([A,B,_,_,_,_]). 
from(A, B) --> state([A,_,B,_,_,_]). 
animal(A, B) --> state([A,_,_,B,_,_]). 
drink(A, B) --> state([A,_,_,_,B,_]). 
smoke(A, B) --> state([A,_,_,_,_,B]). 

X = Y --> { 
    X=..[Fx|Ax], Y=..[Fy|Ay], 
    Xs=..[Fx,S|Ax], Ys=..[Fy,S|Ay] 
}, call(Xs), call(Ys). 

next_to(X, Y) --> {1 is abs(X-Y)}. 
+0

太棒了!目前,這也只能產生「錯誤」。 – mat

+0

@mat:我留下了修正'%next_to(HORSE,黃色)'。信譽爲狩獵確實Enigmativity – CapelliC