问题
I'm trying to solve the Einstein riddle using Prolog. When I'm trying to run by houses(Hs), it shows No. Task is
- The Brit lives in the red house.
- The Swede keeps dogs as pets.
- The Dane drinks tea.
- The green house is on the immediate left of the white house.
- The green house's owner drinks coffee.
- The owner who smokes Pall Mall rears birds.
- The owner of the yellow house smokes Dunhill.
- The owner living in the center house drinks milk.
- The Norwegian lives in the first house.
- The owner who smokes Blends lives next to the one who keeps cats.
- The owner who keeps the horse lives next to the one who smokes Dunhill.
- The owner who smokes Bluemasters drinks beer.
- The German smokes Prince.
- The Norwegian lives next to the blue house.
- 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
回答1:
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).
来源:https://stackoverflow.com/questions/36816529/einstein-riddle-using-prolog