在 Prolog 中搜索原子 [英] Search for a Atom in Prolog

查看:39
本文介绍了在 Prolog 中搜索原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最多的复合词,例如:

I have most Compound Term, such as:

likes(a, b).
likes(c, b).
likes(a, d).
likes(b, c).
likes(c, a).
likes(a, f).
go(a, t).
go(t, d).
go(g, a).
go(f, g).
go(f, a).

我要搜索Compound Tern,Atom的值为a",另一个Atom的值为任意值.

I want to search Compound Tern, value of Atom is "a", value of the other Atom is any value.

Prolog 返回结果:

The result of Prolog return:

likes(a, b).
likes(a, d).
likes(c, a).
likes(a, f).
go(a, t).
go(g, a).
go(f, a).

请帮帮我

推荐答案

首先,您需要具体化"谓词项.你的理论变成:

First you need to "reify" the predicate term. Your theory becomes:

do(likes, a, b).
do(likes, c, b).
do(likes, a, d).
do(likes, b, c).
do(likes, c, a).
do(likes, a, f).
do(go, a, t).
do(go, t, d).
do(go, g, a).
do(go, f, g).
do(go, f, a).

添加以下内容:

has_value(X) :- do(_, X, _)
has_value(X) :- do(_, _, X)

大功告成.

你可以使用像 =.. 这样的元谓词来避免改变你的理论.我个人认为这是错误的方法.最好从一开始就对问题进行正确的形式化.

You could use meta-predicates like =.. to avoid changing your theory. Personally I think that's the wrong approach. Better to have the right formalisation of the problem from the start.

这篇关于在 Prolog 中搜索原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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