2013-10-21 63 views
1

我在學習Prolog,但是我陷入了這個問題,我真的不知道如何完成它。我認爲我的代碼是正確的,但我一直在Sictus發出警告,我不知道我的錯誤在哪裏。在Prolog中解決邏輯謎題

這裏的問題:

/* 
    File:  fall_dance_competition.pl 
    Purpose: Solve the Fall "Dance Competition" puzzle 

    Notes: 
    Saturday night, four couples went to the town’s annual fall dance 
    competition. Each couple dressed in matching colors and they all 
    performed very well. The competition was fierce but friendly and 
    everyone had a grand time. Determine the name of each couple, 
    their place in the competition, and the color each couple wore. 

    1. Tom, who wasn’t married to Jean, placed better than Mary 
     and her husband but worse than the West couple did. 
    2. The couple wearing brown won the event. Mr. and Mrs. 
     King came in 4th place. 
    3. Bill Snow and his wife didn’t wear the green outfits. 
     Jean wasn’t married to John. 
    4. The couple wearing blue placed after the couple wearing red 
     but before the King couple. 
    5. From 4th place to 1st place, the couples won in the following 
     order: John and his wife, the 
     couple wearing blue, Mr. and Mrs. Forest, Brenda and her husband. 
    6. George and his wife placed better than Tom and Sara. 
*/ 

這裏是我的代碼:

use_module(library(basics)).use_module(library(lists)). 

lista([pareja(_,_,_,_,_),pareja(_,_,_,_,_), 
     pareja(_,_,_,_,_),pareja(_,_,_,_,_)]). 

constraints(S) :-  
    member(pareja(tom, _, _, _, _), S) 
    member(pareja(_, jean, _, _, _), S) 
    member(pareja(_, mary, _, _, _), S) 
    member(pareja(_, _, west, _, _), S) 
    perm2(Left_1, Right_1, pareja(tom, _, _, _, _), pareja(_, mary, _, _, _)) 
    nextto(Left_1, Right_1, S) 
    perm2(Left_2, Right_2, pareja(_, _, west, _, _), pareja(tom, _, _, _, _)) 
    nextto(Left_2, Right_2, S) 
    S = [pareja(_, _, _, 1, brown), _, _, _] 
    S = [_, _, _, pareja(_, _, king, 4, _)] 
    member(pareja(bill, _, snow, _, _), S) 
    member(pareja(_, _, _, _, green), S) 
    member(pareja(john, _, _, _, _), S) 
    nextto(pareja(_, _, _, _, red), pareja(_, _, _, _, blue), S) 
    nextto(pareja(_, _, _, _, blue), pareja(_, _, king, _, _), S) 
    S = [_, _, _, pareja(john, _, _, 4, _)] 
    S = [_, _, pareja(_, _, _, 3, blue), _] 
    S = [_, pareja(_, _, forest, 2, _), _, _] 
    S = [pareja(_, brenda, _, 1, _), _, _, _] 
    member(pareja(george, _, _, _, _), S) 
    member(pareja(tom, sara, _, _, _), S) 
    nextto(pareja(george, _, _, _, _), pareja(tom, sara, _, _, _), S). 

green(Who) :- 
    lista(S), 
    constraints(S), 
    member(pareja(_, Who, _, _, green), S). 

nextto(X, Y, List) :- append(_, [X,Y|_], List). 

append([X|Y],Z,[X|W]) :- append(Y,Z,W). 
append([],X,X). 

member(X,[X|T]). 
member(X,[H|T]) :- member(X,T). 

perm2(X,Y, X,Y). 
perm2(X,Y, Y,X). 

我用斑馬難題的一些例子來做到這一點。

+1

你忘了逗號!成員(pareja(tom,_,_,_,_),S)**,**等等 – CapelliC

回答

0

更改程序的第一行:

:- use_module(library(basics)). 
:- use_module(library(lists)). 

檢查是否能解決這個問題。 use_module/1是一個指令,必須這樣寫。

0

你以某種方式錯過了你的constrains/1謂詞末尾的許多逗號。這會在嘗試運行文件時產生很多錯誤。

它應該是這樣的:

constraints(S) :-  
    member(pareja(tom, _, _, _, _), S), 
    member(pareja(_, jean, _, _, _), S), 
    member(pareja(_, mary, _, _, _), S), 
    member(pareja(_, _, west, _, _), S), 
    perm2(Left_1, Right_1, pareja(tom, _, _, _, _), pareja(_, mary, _, _, _)), 
    nextto(Left_1, Right_1, S), 
    perm2(Left_2, Right_2, pareja(_, _, west, _, _), pareja(tom, _, _, _, _)), 
    nextto(Left_2, Right_2, S), 
    S = [pareja(_, _, _, 1, brown), _, _, _], 
    S = [_, _, _, pareja(_, _, king, 4, _)], 
    member(pareja(bill, _, snow, _, _), S), 
    member(pareja(_, _, _, _, green), S), 
    member(pareja(john, _, _, _, _), S), 
    nextto(pareja(_, _, _, _, red), pareja(_, _, _, _, blue), S), 
    nextto(pareja(_, _, _, _, blue), pareja(_, _, king, _, _), S), 
    S = [_, _, _, pareja(john, _, _, 4, _)], 
    S = [_, _, pareja(_, _, _, 3, blue), _], 
    S = [_, pareja(_, _, forest, 2, _), _, _], 
    S = [pareja(_, brenda, _, 1, _), _, _, _], 
    member(pareja(george, _, _, _, _), S), 
    member(pareja(tom, sara, _, _, _), S), 
    nextto(pareja(george, _, _, _, _), pareja(tom, sara, _, _, _), S).