1
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 示例除了一些使用的原子和一些涉及的逻辑之外......有人知道我做错了什么吗?

4

1 回答 1

0

似乎有交换的字段。将三元组更改为此

Triples= [[gertie,GertEx,Y1],
      [herbert,HerbEx,Y2],
      [miriam,MirEx,Y3],
      [wallace,WallEx,Y4]],

我明白了

?- solve.
gertie made the 1928 exchange in be
herbert made the 1929 exchange in pe
miriam made the 1932 exchange in sl
wallace made the 1935 exchange in al
true .
于 2013-05-02T10:54:55.447 回答