Prolog delete:不删除与Element统一的所有元素 [英] Prolog delete: doesn't delete all elements that unify with Element

查看:10
本文介绍了Prolog delete:不删除与Element统一的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 SWI-Prolog 的 delete/3 谓词时遇到问题.最简单的方法只是一个简单的例子:

?- delete([(1,1),(1,2),(3,2)], (1,_), 列表).列表 = [(1,2),(3,2)].

我希望 (1,2) 也被删除,因为 (1,_)(1,2) 统一.SWIPL 帮助说:

<块引用>

删除List1所有成员同时与Elem合一,结果与List2合一.

为什么会这样?如何删除与 (1,_) 统一的所有内容?

解决方案

" 删除List1中同时与Elem合并的所有成员,并将结果与​​List2合并."

(1,X) 首先与 (1,1) 合一.因此,X与1合一,不能与2合一删除(1,2).所以问题不在于它没有删除所有的成员;是它没有与(1,2)和(1,1)同时统一(尝试删除([(1,1),(1,2),(1,1),(3,2)],(1,_),List).

顺便说一句,根据 swi-prolog 手册:

<块引用><块引用>

删除(?List1, ?Elem, ?List2)
当 Lis1 时为真,所有出现的 Elem 被删除导致 List2 中.

另外,delete/3 已被弃用:

<块引用>

有太多的方法可以用来从列表中删除元素来证明名称的合理性.考虑匹配(= vs. ==),首先/全部删除,是否确定.

所以最简单的方法是编写自己的谓词.比如:

my_delete(Pattern,[Pattern|T],TD):-my_delete(模式,T,TD).my_delete(模式,[H|T],[H|TD]):-my_delete(模式,T,TD).

也许?

检查 排除/3、包含/3、分区/4

I'm having an issue with SWI-Prolog's delete/3 predicate. The easiest way is just a quick example:

?- delete([(1,1),(1,2),(3,2)], (1,_), List).
List = [(1,2),(3,2)].

I would expect (1,2) to also be deleted, since (1,_) unifies with (1,2). The SWIPL help says:

Delete all members of List1 that simultaneously unify with Elem and unify the result with List2.

Why is this and how can I delete everything that unifies with (1,_)?

解决方案

" Delete all members of List1 that simultaneously unify with Elem and unify the result with List2."

(1,X) first unifies with (1,1). therefore, X is unified with 1 and cannot be unified with 2 to delete (1,2). so the problem is not that it does not delete all of the members; it's that it doesnt unify simultaneously with (1,2) and (1,1) (try delete([(1,1),(1,2),(1,1),(3,2)],(1,_),List).

btw, according to the swi-prolog manual:

delete(?List1, ?Elem, ?List2)
Is true when Lis1, with all occurences of Elem deleted results in List2.

also, delete/3 is deprecated:

There are too many ways in which one might want to delete elements from a list to justify the name. Think of matching (= vs. ==), delete first/all, be deterministic or not.

So the easiest way is to write your own predicate. Something like:

my_delete(Pattern,[Pattern|T],TD):-
   my_delete(Pattern,T,TD).
my_delete(Pattern,[H|T],[H|TD]):-
   my_delete(Pattern,T,TD).

perhaps?

check exclude/3, include/3, partition/4

这篇关于Prolog delete:不删除与Element统一的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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