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([_]).

I try to run it but all it returns is a false statement. I got the question from http://www.cis.upenn.edu/~matuszek/cis554-2012/Assignments/prolog-01-logic-puzzle.html and from what I can see it's almost exactly the same as the it's-a-tie.pl sample apart from some of the atoms used and some of the logic involved...anybody have any idea what I'm doing wrong?

有帮助吗?

解决方案

Seems that there are swapped fields. Changing Triples to this

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

I get

?- 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 .
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top