简体   繁体   English

解决Prolog中的逻辑难题

[英]Solving a Logic Puzzle in Prolog

I'm learning Prolog, but I got stuck with this problem and I really don't know how to finish it. 我正在学习Prolog,但是遇到了这个问题,我真的不知道该如何完成。 I thought that I was right in my code, but I keep getting warnings in Sictus, and I don't know where my mistake is. 我以为我的代码正确,但是我在Sictus中不断收到警告,而且我不知道我的错误在哪里。

Here's the problem: 这是问题所在:

/*
    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.
*/

And here is my code: 这是我的代码:

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).

I used some examples of the Zebra puzzle to do it. 我用斑马拼图的一些例子来做。

Change the first line of your program to: 将程序的第一行更改为:

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

Check if that solves the problem. 检查是否可以解决问题。 use_module/1 is a directive and must be written as such. use_module/1是指令,必须这样编写。

You somehow missed many commas in the end of your constrains/1 predicate. 您在constrains/1谓词的末尾以某种方式错过了许多逗号。 This will produce a lot of error when trying to run the file. 尝试运行文件时,这将产生很多错误。

It should be like that: 应该是这样的:

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).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM