year('1928').
year('1929').
year('1932').
year('1935').
person(gertie).
person(herbert).
person(miriam).
person(wallace).
exchange(al).
exchange(be).
exchange(pe).
exchange(sl).
solve:- year(Y1), year(Y2), year(Y3), year(Y4),
unique([Y1,Y2,Y3,Y4]),
exchange(GertEx), exchange(HerbEx), exchange(MirEx), exchange(WallEx),
unique([GertEx,HerbEx,MirEx,WallEx]),
Triples= [[gertie,Y1,GertEx],
[herbert,Y2,HerbEx],
[miriam,Y3,MirEx],
[wallace,Y4,WallEx]],
%herberts first exchange was BE
\+ member([herbert,be,_],Triples),
%neither herberts nor gerties first exchange was sl
\+ (
member([herbert,sl,_],Triples);
member([gertie,sl,_],Triples)
),
%the BE exchange wasnt made in 1935
\+ member([_,be,'1935'],Triples),
%neither the AL nor the BE exchanges were made in 1932
\+ (
member([_,al,'1932'],Triples);
member([_,be,'1932'],Triples)
),
%the PE exchange was made in 1929
member([_,pe,'1929'],Triples),
%wallace's first exchange was AL
member([wallace,al,_],Triples),
writer(gertie,Y1,GertEx),
writer(herbert,Y2,HerbEx),
writer(miriam,Y3,MirEx),
writer(wallace,Y4,WallEx).
writer(X,Y,Z):- write(X), write(' made the '), write(Y), write(' exchange in '), write(Z), nl.
unique([H | T]) :- member(H, T), !, fail.
unique([_ | T]) :- unique(T).
unique([_]).
我嘗試運行它,但它返回的是一個虛假的聲明。 我從http://www.cis.upenn.edu/~matuszek/cis554-2012/Assignments/prolog-01-logic-puzzle.html那裏得到了問題,從我看到的幾乎和它的-a-tie.pl樣本一樣,除了一些使用的原子和一些涉及的邏輯之外......任何人都知道我是什麼做錯了嗎?電話對話邏輯謎題{Prolog}
你試過跟蹤執行,以發現自己的錯誤嗎? – 2013-05-02 10:34:54
我如何追蹤? 抱歉,幾乎沒有使用prolog一個月... – 2013-05-02 10:37:09
在SWI-Prolog中,在交互式頂層中發出trace.'然後解析'(最後關閉跟蹤'notrace.'和'nodebug.' )。您可以通過Enter進行推理並用's'跳過部分。 – 2013-05-02 10:39:50