Prolog,使用关系后输出变量



这是我第一天学习的序言,我想写一个程序,根据天气和我在办公室的约会来决定我应该穿什么鞋。主要"功能"例如:

"go :- outfit(snow, 15, casual, F1), write(F1)."

雪是天气,15是温度(现在不相关),casual是约会的形式。"写(F1)";将显示"输出"。所以变量F1需要是上述关系的结果。以下是穿什么鞋的规则:

%Rules for weather
rain(Weather) :- (Weather = 'rain').
snow(Weather) :- (Weather = 'snow').
nice(Weather) :- (Weather = 'nice').
clear(Weather) :- (Weather = 'clear').
%Rules for formality
formal(Appointment) :- (Appointment = 'formal').
semiformal(Appointment) :- (Appointment = 'semiformal').
casual(Appointment) :- (Appointment = 'casual').
%Rules for when to wear a type of footwear
dressShoes(Appointment) :- formal(Appointment).
boots(Appointment, Weather) :- not(formal(Appointment)), (rain(Weather);snow(Weather)).
sneakers(Appointment, Weather) :- not(formal(Appointment)), (nice(Weather);clear(Weather)).

这就是我的问题所在,我不确定如何将最后三个关系与填充变量"F1"的单个关系联系起来。最后一套"服装"函数。我是一个c++爱好者,所以我想在F1中放入一个字符串,比如"[sneakers]"或";(靴)";但这是我对prolog的成长烦恼的一部分。如有任何帮助,不胜感激。

我猜是对Prolog的一些误解。这种规则:

rule(Variable) :- (Variable = 'value').

您不需要引用'value',它已经是原子。无论你读什么书,都要查一下原子。它变成了:

rule(Variable) :- (Variable = value).

在规则定义中不需要额外的圆括号。它变成了:

rule(Variable) :- Variable = value.

你不需要显式的正文统一。在头部和身体之间没有别的东西发生。所以也不需要变量。它变成了:

rule(value).

应用到你的程序中,我得到:

rain(rain).
snow(snow).
nice(nice).
clear(clear).
%Rules for formality
formal(formal).
semiformal(semiformal).
casual(casual).

这些规则几乎什么也没说;-)

你的例子在最上面:

go :- outfit(snow, 15, casual, F1), write(F1).

是否与仅仅调用outfit(snow, 15, casual, F1)完全相同?那么"去"的目的是什么呢?而"写"字呢?我想跳过它们。

程序的逻辑:不用代码你能解释吗?你的代码太不寻常了,我只能猜了。

如果你想说"如果约会是正式的,请穿正装鞋",你可以这样写:

occasion_shoes(Occasion, Shoes) :-
formality_occasion(Formality, Occasion),
formality_shoes(Formality, Shoes).
formality_occasion(formal, evening_party).
formality_occasion(semi_formal, office).
formality_shoes(formal, dress_shoes).

你明白是怎么回事吗?你要把场合和鞋子搭配起来。要做到这一点,您可以在表formality_occasion/2中查找场合的正式程度,然后将其与formality_shoes/2表中鞋子的正式程度相匹配。

如果你正在努力为你的问题建模,你也可以阅读关系数据库设计。

最新更新