序言与逻辑难题 [英] Prolog and Logic Puzzles

查看:94
本文介绍了序言与逻辑难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对Prolog中事实的统一存在疑问,但无法确认.一切看起来都应该有效,并且我已经看了使用Prolog解决逻辑难题的例子,但鉴于Prolog相对稀有,因此没有任何实际效果.

I seem to be having an issue with the unification of facts in Prolog, but can't confirm it. Everything looks like it should work, and I've looked up examples of solved logic puzzles using Prolog to no real effect, given the relative rarity of Prolog.

是一项额外的功劳分配,所以我不确定它是否有效,但是我真的很困惑如何从此处开始

This is an extra credit assignment, so I'm unsure if it's valid, but I'm genuinely stumped on how to proceed from here

% Names
name(teo).
name(mira).
name(bruno).
name(igor).

%Food
food(sandwich).
food(pie).
food(hamburger).
food(pizza).

%Hobby
hobby(crosswords).
hobby(writing).
hobby(reading).
hobby(photography).

%Shirt Colour
shirt(white).
shirt(yellow).
shirt(blue).
shirt(red).

%Other
girl(mira).

student((Name, Food, Hobby, Shirt)) :-
    name(Name), food(Food), hobby(Hobby), shirt(Shirt).

solution(L):-
    length(L,4),
    L= [student(teo, sandwich,_,_),_,_,_],
    member(student(mira, pite, crosswords,_),L),
    member(student(girl(GirlName),_,_,white),L),
    member(student(bruno,_,_,yellow),L),
    member(student(_,hamburger,writing,_),
    L= [_, student(_,pie,_,_),_,_],
    next(student(_,pie,_,_), student(teo,_,_,_), L),
    next(student(bruno,_,_,_), student(_,pizza,_,_), L),
    next(student(_,_,_,white), student(_,pizza,_,_), L),
    member(student(igor,_,reading,_),L),
    next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L).

next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).

问题在于,它不会将solution(L)当作谓词或规则来对待,而只是将其视为一个文本块,因此我什至无法测试它是否正确.我最感兴趣的是为什么它甚至不起作用.

The issue is that it won't treat solution(L) as if it's a predicate or a rule, simply a block of text, so I can't even test if it's correct or not. I am most interested in why it won't even function.

推荐答案

起初,我以为这个girl/1是许多人提到的罪魁祸首.但是,即使删除所有此类情况,您的定义仍然会失败(并且在修复该语法错误之后).这是solution(L)仍然失败的负责部分:

At first, I thought this girl/1 is the culprit as many have remarked. But even when removing all such occurrences your definition still fails (and after fixing that syntax error). Here is the responsible part that still fails for solution(L):


:- op(950, fy, *).
*(_).

solution(L) :-
   * length(L,4),
   L= [student(_/*teo*/, sandwich,_,_),_,_,_],
   member(student(_/*mira*/, pite, _/*crosswords*/,_),L),
   * member(student(girl(GirlName),_,_,white),L),
   * member(student(bruno,_,_,yellow),L),
   member(student(_,hamburger,_/*writing*/,_),L),
   L= [_, student(_,pie,_,_)|_/*[_,_]*/],
   * next(student(_,pie,_,_), student(teo,_,_,_), L),
   next(_/*student(bruno,_,_,_)*/, student(_,pizza,_,_), L),
   * next(student(_,_,_,white), student(_,pizza,_,_), L),
   * member(student(igor,_,reading,_),L),
   * next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L).

next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).

所有名字都和他们的爱好无关.真正重要的是食物!

All the names are irrelevant, just as their hobbies. What really matters is the food!

您只有四个地方,但是总共有五种食物(三明治,派,皮特,汉堡包,披萨)-仅给我其中一种!

You have only four places, but a total of five foods (sandwich, pie, pite, hamburger, pizza) - give me just one of them!

这在Prolog中是一件好事:您可以进行这样的概括以获得明确的诊断.

That's the nice thing in Prolog: You can do such generalizations to get a clean diagnosis.

这篇关于序言与逻辑难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆