different/2 - 是否存在纯粹的、确定的定义? [英] different/2 - does a pure, determinate definition exist?

查看:15
本文介绍了different/2 - 是否存在纯粹的、确定的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

different(Xs, Ys) :-
   member(X, Xs),
   non_member(X, Ys).
different(Xs, Ys) :-
   member(Y, Ys),
   non_member(Y, Xs).

虽然此定义使用 member/2non_member/2 几乎是1 从声明的角度来看是完美的,它为某些查询生成冗余解决方案,并在周围留下选择点.

While this definition using member/2 and non_member/2 is almost1 perfect from a declarative viewpoint, it produces redundant solutions for certain queries and leaves choice points all around.

什么是对此进行改进的定义(以纯粹的方式可能使用 if_/3(=)/3) 以便描述完全相同的解决方案集通过 different/2 但至少对于地面查询是确定的(因此不会留下任何无用的选择点)并省略(如果可能)任何多余的答案?

What is a definition that improves upon this (in a pure manner probably using if_/3 and (=)/3) such that exactly the same set of solutions is described by different/2 but is determinate at least for ground queries (thus does not leave any useless choice points open) and omits (if possible) any redundant answer?

1实际上, different([a|nonlist],[]), different([],[b|nonlist]) 成功了.它同样可能失败.因此,两者都失败的解决方案很好(甚至更好).

1 Actually, different([a|nonlist],[]), different([],[b|nonlist]) succeeds. It could equally fail. So a solution that fails for both is fine (maybe even finer).

推荐答案

让我们把它发挥到极致---在 的帮助下list_nonmember_t/3exists_in_t/3、和or_/2

Let's take it to the limit---by the help of list_nonmember_t/3, exists_in_t/3, and or_/2!

some_absent_t(Xs,Ys,Truth) :-
   exists_in_t(list_nonmember_t(Ys),Xs,Truth).

different(Xs,Ys) :-
   or_(some_absent_t(Xs,Ys),
       some_absent_t(Ys,Xs)).

这篇关于different/2 - 是否存在纯粹的、确定的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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