Prolog delete:不删除与Element统一的所有元素 [英] Prolog delete: doesn't delete all elements that unify with 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).
也许?
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 withElem
and unify the result withList2
.
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屋!