prolog - Prolog,使用关系后输出变量
问题描述
这是我第一天的学习序言,我想编写一个程序,根据天气和我在办公室的约会类型来决定我应该穿什么鞋。主要的“功能”将被声明,例如:
"go :- outfit(snow, 15, casual, F1), write(F1)."
雪是天气,15是温度(现在不相关),随便是约会的形式。“write(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]”或“[boots]”,但这是我在 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
表中鞋子的形式相匹配。
如果您正在努力为您的问题建模,您还可以阅读关系数据库设计。
推荐阅读
- python - ValueError: multi_class 必须在 ('ovo', 'ovr')
- export-to-excel - 将 SAP 元数据导出到 Excel 的方法?
- inno-setup - 安装程序无法恢复损坏的文件类型关联
- azureservicebus - 简化 Azure 服务总线中 MassTransit Saga 的设置
- javascript - 单击按钮时 Cookie 未触发
- php - Laravel 8 - SQLSTATE[HY000] [2002] 没有这样的文件或目录
- javascript - Mongoose 从文件中导出和导入 MongoDB 文档
- javascript - 使用firebase创建帐户时如何为用户添加DisplayName?
- html - 如何制作两列?
- javascript - 在 react 和 javascript 中,如何正确映射此对象的键值?