prologzebra-puzzle

Variation of the zebra puzzle in prolog, and I can't figure out where I'm going wrong


I know the zebra puzzle is asked often here, but this is a bit different: We were given a variation of the zebra puzzle in Prolog to write. I'm super new to this, but I even tried getting help from some people last year who had themselves a different variation, and they weren't sure what was going on with my code.

I'll post the whole thing, I hope it's not overwhelming or bad practice.

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).

My database loads just fine, but results in false when I try to run anything. If I run dorms(Dorms) to print out all the results, I am returned false. When I run csMusic (trying to find the type of music the CS major listens to) or engDrink (trying to find what kind of drink the English major drinks), I am returned false.

I know quite little of Prolog, but I did my best to follow along on the swipl website when going over this problem. Is there perhaps something easy I'm missing that anyone could point out? An inconsistency in naming, perhaps?

Help is much appreciated, thanks!


Solution

  • In clause 8, I didn't actually mention tea or Tesla. In clause 10, my first use of not had the incorrect number of items in the array. In clauses 14 and 17, I misspelled dorm as 'dorms'.

    Fixing these, I was able to receive the correct and complete answer.