0

ここでシマウマのパズルがよく聞かれることは知っていますが、これは少し違います。Prolog でシマウマのパズルのバリエーションを書いてもらいました。私はこれに非常に慣れていませんが、昨年、別のバリエーションを持っている何人かの人々から助けを得ようとしましたが、彼らは私のコードで何が起こっているのか確信が持てませんでした.

全体を投稿します。圧倒されたり、悪い習慣ではないことを願っています。

register_renderer/2

:- use_rendering(table,
         [header(dorm('Major', 'Car', 'Team', 'Music', 'Drink'))]).

csMusic(Music) :-
  dorms(Dorms),
  member(dorm(computerscience,_,_,Music,_),Dorms).

engDrink(Drink) :-
  dorms(Dorms),
  member(dorm(english,_,_,_,Drink),Dorms).

dorms(Dorms) :-
      length(Dorms, 5),

      % 1.  The computer science student lives in the middle of the corridor.
      Dorms = [_,_,(dorm(computerscience,_,_,_,_)),_,_],
      % 2.  The history major is a jazz fan.
      member(dorm(history,_,_,jazz,_),Dorms),
      % 3.  The Yankees fan drives a Toyota.
      member(dorm(_,toyota,yankees,_,_),Dorms),
      % 4.  The accounting major drinks Coke.
      member(dorm(accounting,_,_,_,coke),Dorms),
      % 5.  The engineering major drinks coffee.
      member(dorm(engineering,_,_,_,coffee),Dorms),
      % 6.  The computer science student and history student are neighbors.
      adjacent((dorm(computerscience,_,_,_,_)),(dorm(history,_,_,_,_)),Dorms),
      % 7.  The student at the far end of the hall likes classical music.
      Dorms = [_,_,_,_,(dorm(_,_,_,classical,_))],
      % 8.  The tea drinker drives a Tesla.
      member(dorm(_,_,_,_,_),Dorms),
      % 9.  The classical music fan lives next to the jazz listener.
      adjacent((dorm(_,_,_,classical,_)),(dorm(_,_,_,jazz,_)),Dorms),
      % 10. The English major does not live in either of the first two rooms.
      member(dorm(english,_,_,_,_),Dorms),
      not(Dorms = [dorm(english,_,_,_,_)]),
      not(Dorms = [_,dorm(english,_,_,_,_),_,_,_]),
      % 11. The Royals fan drives a Tesla.
      member(dorm(_,tesla,royals,_,_),Dorms),
      % 12. The Cubs fan listens to jazz.
      member(dorm(_,_,cubs,jazz,_),Dorms),
      % 13. The engineering major follows the Chiefs
      member(dorm(engineering,_,chiefs,_,_),Dorms),
      % 14. The first room is the home of the Broncos fan
      Dorms = [dorms(_,_,broncos,_,_),_,_,_,_],
      % 15. The Coke drinker drives a Nissan.
      member(dorm(_,nissan,_,_,coke),Dorms),
      % 16. The country music fan and the techno fan are neighbors.
      adjacent((dorm(_,_,_,country,_)),(dorm(_,_,_,techno,_)),Dorms),
      % 17. The accounting major lives in the first room.
      Dorms = [dorms(accounting,_,_,_,_),_,_,_,_],
      % 18. The fans of the 2 Kansas City teams (Chiefs and Royals) are neighbors
      adjacent((dorm(_,_,chiefs,_,_)),(dorm(_,_,royals,_,_)),Dorms),
      % 19. The accounting major listens to rock music
      member(dorm(accounting,_,_,rock,_),Dorms),
      % 20. The Yankees fan drinks milk.
      member(dorm(_,_,yankees,_,milk),Dorms),
      % 21. The Chevy driver listens to country music.
      member(dorm(_,chevy,_,country,_),Dorms),
      % 22. The jazz fan drives a Ford.
      member(dorm(_,ford,_,jazz,_),Dorms),
      % 23. Water isnt used.
      member(dorm(_,_,_,_,water),Dorms).

next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).
adjacent(A,B,List) :- next(A,B,List); next(B,A,List).

私のデータベースは正常に読み込まれますが、何かを実行しようとすると false になります。すべての結果を印刷するために実行dorms(Dorms)すると、 が返されfalseます。csMusic(CS 専攻が聴いている音楽の種類を見つけようとして) またはengDrink(英語専攻がどんな種類の飲み物を飲んでいるかを見つけようとして) 実行すると、返されますfalse.

私は Prolog についてほとんど知りませんが、この問題を解決するときは、 swiplの Web サイトをフォローするために最善を尽くしました。誰かが指摘できる、私が見逃している簡単なものはありますか?ネーミングの矛盾?

助けていただければ幸いです。

4

2 に答える 2