2

I'm trying to solve the Einstein riddle using Prolog. When I'm trying to run by houses(Hs), it shows No. Task is

  1. The Brit lives in the red house.
  2. The Swede keeps dogs as pets.
  3. The Dane drinks tea.
  4. The green house is on the immediate left of the white house.
  5. The green house's owner drinks coffee.
  6. The owner who smokes Pall Mall rears birds.
  7. The owner of the yellow house smokes Dunhill.
  8. The owner living in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The owner who smokes Blends lives next to the one who keeps cats.
  11. The owner who keeps the horse lives next to the one who smokes Dunhill.
  12. The owner who smokes Bluemasters drinks beer.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The owner who smokes Blends lives next to the one who drinks water.
    houses(Hs) :-
       length(Hs, 5),                                            
       member(h(english,_,_,_,red), Hs),                         
       member(h(swede,dog,_,_,_), Hs),                         
       member(h(_,_,_,coffee,green), Hs),                        
       member(h(dane,_,_,tea,_), Hs),                       
       next(h(_,_,_,_,green), h(_,_,_,_,white), Hs),             
       member(h(_,bird,'Pall Mall',_,_), Hs),                       
       member(h(_,_,'Dunhill',_,yellow), Hs),                         
       Hs = [_,_,h(_,_,_,milk,_),_,_],                           
       Hs = [h(norwegian,_,_,_,_)|_],                            
       next(h(_,horse,_,_,_), h(_,_,'Dunhill',_,_), Hs),        
       next(h(_,_,blend,_,_), h(_,cat,_,_,_), Hs),             
       member(h(_,_,'Blue Master',beer,_), Hs),                        
       member(h(german,_,'Prince',_,_), Hs),                      
       next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs),  
       next(h(_,_,'Blend',_,_), h(_,_,_,water,_), Hs), 
       member(h(_,fish,_,_,_), Hs).

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

I have no idea what is wrong. Thanks

false
  • 10,264
  • 13
  • 101
  • 209
user3637775
  • 499
  • 4
  • 20
  • 1
    Where is `next` defined? It's not a built-in SWI Prolog predicate. – lurker Apr 23 '16 at 21:56
  • I'm sorry I forgot to add it. next(A, B, Ls) :- append(_, [A,B|_], Ls). next(A, B, Ls) :- append(_, [B,A|_], Ls). – user3637775 Apr 24 '16 at 06:51
  • 1
    When I'm trying to run houses(Hs). It shows No. I have no idea why. – user3637775 Apr 24 '16 at 06:55
  • @user3637775 - You probably mistranslated a rule. Can you post the original puzzle text? – Enigmativity Apr 24 '16 at 07:40
  • by now you must have realized that you gave 6 different smokes specifications for the 5-long list: `'Pall Mall', 'Dunhill', blend, 'Blue Master', 'Prince'` and `'Blend'`. There's no way to fit them all into it. Otherwise, it is very easy to make an error hand-translating the rules into those `h(....)` specs; [better use Prolog](http://stackoverflow.com/questions/9252656/einsteins-riddle-prolog/20092493#20092493) for that [too](http://stackoverflow.com/questions/20176445/riddle-with-gnu-prolog-similar-to-einstein-riddle/20408402#20408402). – Will Ness May 08 '16 at 22:48
  • Possible duplicate of [Einstein Riddle with List of terms](http://stackoverflow.com/questions/36743498/einstein-riddle-with-list-of-terms) –  Dec 20 '16 at 23:20

1 Answers1

5

Here is a generalization of your program. I added some extra * to remove several goals and replaced some terms by _/*origterm*/. And yet, the resulting program is still failing. Therefore, the error has to be in the remaining fragment. You did not say anything about the program (Edit: you added something later), so I do not (Edit: want to) know what it is about. But no matter what, the error has to be in the remaining visible part:

:- initialization(houses(_Sol)).
:- op(950, fy, *).
*_.

houses(Hs) :-
   length(Hs, 5),
   * member(h(english,_,_,_,red), Hs),                         %  2
   * member(h(swede,dog,_,_,_), Hs),
   * member(h(_,_,_,coffee,green), Hs),
   * member(h(dane,_,_,tea,_), Hs),
   * next(h(_,_,_,_,green), h(_,_,_,_,white), Hs),
   member(h(_,_/*bird*/,'Pall Mall',_,_), Hs),
   member(h(_,_,'Dunhill',_,_/*yellow*/), Hs),
   * Hs = [_,_,h(_,_,_,milk,_),_,_],
   * Hs = [h(norwegian,_,_,_,_)|_],
   * next(h(_,horse,_,_,_), h(_,_,'Dunhill',_,_), Hs),
   next(h(_,_,blend,_,_), _/*h(_,cat,_,_,_)*/, Hs),
   member(h(_,_,'Blue Master',_/*beer*/,_), Hs),
   member(h(_/*german*/,_,'Prince',_,_), Hs),
   * next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs),
   next(h(_,_,'Blend',_,_), _/*h(_,_,_,water,_)*/, Hs),
   * member(h(_,fish,_,_,_), Hs).

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

There is not much left! In the visible part there is at least one error! (And, strictly speaking there may be many more errors in the other parts. We simply don't know).

false
  • 10,264
  • 13
  • 101
  • 209