在序言中分组两个事实的规则? [英] Rule to group two facts in prolog?

查看:44
本文介绍了在序言中分组两个事实的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为伦敦地铁编写代码.我已经声明了显示车站名称及其所在线路的事实.例如

station(aldgate,metropolitan).车站(布里克斯顿,维多利亚).车站(面包师,大都会).

我正在尝试制定一个规则来检查两个车站是否在同一条线上,以及那条线.例如,aldgatebaker 在同一行,metropolitan.

有什么想法吗?

解决方案

我正在尝试制定一个规则来检查两个车站是否在同一条线上,以及那条线.

一个例子可以是以下规则

sameLine(Stat1, Stat2, Line) :-车站(Stat1, Line),车站(Stat2, Line),统计 1 \= 统计 2.

那很灵活.

它可以检查几个车站是否在同一条线上(调用sameLine(aldgate,baker,metropolitan)返回true,调用sameLine(aldgate,baker,Line)code> 返回 true 并将 Linemetropolitan 统一)但可以找到一条线的几个车站(调用 sameLine(Stat1, Stat2, Metropolitan)两次返回 true,将 Stat1aldgateStat2baker 统一(第一次),反之亦然(第二次)))

遵守约束

Stat1 \= Stat2.

就是强加两个站不一样.

如果您希望 sameLine(aldgate, aldgate, Line) 返回 true 将 Linemetropolitan 统一起来,您可以将其删除.>

如果您想避免双重结果(aldgate/bakerbaker/aldgate,例如,调用 sameLine(Stat1, Stat2, Metropolitan)) 你可以强加 Stat1 不仅与 Stat2 不同,而且也就是之前"Stat2,替换

Stat1 \= Stat2

Stat1 @<状态2

但是,通过这种方式,您可以从 sameLine(aldgate, baker, Line) 获得 true,但为 false(因为 baker 不是之前"aldgate) 来自 sameLine(baker, aldgate, Line).

I am writing code for the London tube. I have declared facts that display the name of a station and also which line it is on. e.g.

station(aldgate,metropolitan).
station(brixton,victoria).
station(baker,metropolitan).

I'm trying to work out a rule that will check whether two stations are on the same line, and which line that is. For example, aldgate and baker are on the same line, metropolitan.

Any ideas?

解决方案

I'm trying to work out a rule that will check whether two stations are on the same line, and which line that is.

An example can be the following rule

sameLine(Stat1, Stat2, Line) :-
  station(Stat1, Line),
  station(Stat2, Line),
  Stat1 \= Stat2.

that is flexible.

It can check of a couple of station is in the same line (calling sameLine(aldgate, baker, metropolitan) return true, calling sameLine(aldgate, baker, Line) return true and unify Line with metropolitan) but can find couples of stations of a line (calling sameLine(Stat1, Stat2, metropolitan) return true two times, unifying Stat1 with aldgate and Stat2 with baker (the first time) and vice versa (the second time)).

Observe the constraint

Stat1 \= Stat2.

It's to impose that the two stations are different.

If you want that sameLine(aldgate, aldgate, Line) return true unifying Line with metropolitan, you can delete it.

If you, otherwise, want to avoid the double results (aldgate/baker and baker/aldgate, by example, calling sameLine(Stat1, Stat2, metropolitan)) you can impose that Stat1 is non only different than Stat2 but also that is "before" Stat2, replacing

Stat1 \= Stat2

with

Stat1 @< Stat2

But, in this way, you obtain true from sameLine(aldgate, baker, Line), but false (because baker isn't "before" aldgate) from sameLine(baker, aldgate, Line).

这篇关于在序言中分组两个事实的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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