我知道斑马谜题在这里经常被问到,但这有点不同:我们在 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).
我的数据库加载得很好,但是当我尝试运行任何东西时会导致错误。如果我跑dorms(Dorms)
打印出所有结果,我会被退回false
。当我跑步csMusic
(试图找到CS专业听的音乐类型)或engDrink
(试图找到英语专业喝什么样的饮料)时,我又回来了false
。
我对 Prolog 知之甚少,但在解决这个问题时,我尽我所能在swipl网站上跟进。是否有任何人可以指出的容易我想念的东西?可能是命名不一致?
非常感谢您的帮助,谢谢!