问题
I'm trying to solve the Einstein riddle using Prolog. Task is
- The Norwegian lives in the first house .
- The English lives in the Red House .
- The Swedish HAS Dogs As pets .
- The Danish drinks tea .
- The Green House is on the left of the White House.
- The man who lives in the green house drinks coffee .
- The man who smokes Pall Mall rears birds .
- The man living in the Yellow House smokes Dunhill .
- The man who lives in the Middle house drinks milk .
- The man who smokes Blends lives next to the one who Has Cats .
- The man who keeps horses lives next to the one who smokes Dunhill .
- The man who smokes Blue Master drinks beer .
- The German smokes Prince .
- The Norwegian lives next to the Blue House side .
- The man who smokes Blends is Neighbour do of the one who drinks water .
- Someone has one aquarium with fish .
The program:
neighbor(Rua):-
length(Rua, 5),
Rua = [casa(_,noruegues,_,_,_)|_],
member(casa(vermelha,ingles,_,_,_),Rua),
member(casa(_,sueco,_,_,cachorros),Rua),
member(casa(_,dinamarques,cha,_,_),Rua),
esquerda(casa(verde,_,_,_,_), casa(branca,_,_,_,_),Rua),
member(casa(verde,_,cafe,_,_),Rua),
member(casa(_,_,_,pallmall,passaros),Rua),
member(casa(amarela,_,_,dunhill,_),Rua),
Rua = [_,_,casa(_,_,leite,_,_),_,_],
ao_lado(casa(_,_,_,blends,_), casa(_,_,_,_,gatos),Rua),
ao_lado(casa(_,_,_,_,cavalos), casa(_,_,_,dunhill,_),Rua),
member(casa(_,_,cerveja,bluemaster,_),Rua),
member(casa(_,alemao,_,prince,_),Rua),
ao_lado(casa(_,noruegues,_,_,_), casa(azul,_,_,_,_),Rua),
ao_lado(casa(_,_,_,blends,_), casa(_,_,agua,_,_),Rua),
member(casa(_,_,_,_,peixes),Rua).
ao_lado([X,Y|_],X, Y).
ao_lado([X,Y|_],Y, X).
ao_lado([_|L],X, Y):-
ao_lado(L, X, Y).
esquerda([A|As], A, E) :-
member2(E, As).
esquerda([_|As], A, E) :-
esquerda(As, A, E).
回答1:
Here is one reason you have to address to solve this problem. Below program fragment has quite a lot of goals removed, yet it still fails. The visible part alone is already responsible for the failure. Can you spot the reason from this fragment?
(For more on this method see this explanation.)
:- op(950, fy, *). *_. :- initialization(neighbor(_Rua)). neighbor(_/*Rua*/):- *length(Rua, 5), *Rua = [casa(_,noruegues,_,_,_)|_], *member(casa(vermelha,ingles,_,_,_),Rua), *member(casa(_,sueco,_,_,cachorros),Rua), *member(casa(_,dinamarques,cha,_,_),Rua), esquerda(casa(_/*verde*/,_,_,_,_), _/*casa(branca,_,_,_,_)*/,Rua), *member(casa(verde,_,cafe,_,_),Rua), *member(casa(_,_,_,pallmall,passaros),Rua), *member(casa(amarela,_,_,dunhill,_),Rua), *Rua = [_,_,casa(_,_,leite,_,_),_,_], *ao_lado(casa(_,_,_,blends,_), casa(_,_,_,_,gatos),Rua), *ao_lado(casa(_,_,_,_,cavalos), casa(_,_,_,dunhill,_),Rua), *member(casa(_,_,cerveja,bluemaster,_),Rua), *member(casa(_,alemao,_,prince,_),Rua), *ao_lado(casa(_,noruegues,_,_,_), casa(azul,_,_,_,_),Rua), *ao_lado(casa(_,_,_,blends,_), casa(_,_,agua,_,_),Rua), *member(casa(_,_,_,_,peixes),Rua). esquerda([A|As], _/*A*/, E) :- *member(E, As). esquerda([_|As], A, E) :- *esquerda(As, A, E).
来源:https://stackoverflow.com/questions/38928611/einstein-puzzle-in-prolog