序言删除:不会删除所有与Element合并的元素 [英] Prolog delete: doesn't delete all elements that unify with Element
问题描述
我对SWI-Prolog的delete/3
谓词有疑问.
最简单的方法只是一个简单的例子:
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)].
我希望(1,2)
也将被删除,因为(1,_)
与(1,2)
统一. SWIPL帮助说:
I would expect (1,2)
to also be deleted, since (1,_)
unifies with (1,2)
. The SWIPL help says:
删除同时与
Elem
统一并与List2
统一的List1
的所有成员.
Delete all members of
List1
that simultaneously unify withElem
and unify the result withList2
.
这是为什么?如何删除与(1,_)
统一的所有内容?
Why is this and how can I delete everything that unifies with (1,_)
?
推荐答案
删除同时与Elem统一的List1的所有成员,并与List2统一结果."
" Delete all members of List1 that simultaneously unify with Elem and unify the result with List2."
(1,X)首先与(1,1)统一.因此,X与1统一,不能与2统一删除(1,2). 因此,问题不在于它不会删除成员的所有;这是因为它不能同时与(1,2)和(1,1)统一 (尝试删除[[((1,1),(1,2),(1,1),(3,2)],(1,_),列表).
(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).
顺便说一句,根据 swi-prolog手册:
delete(?List1,?Elem,?List2)
当Lis1以及所有出现的Elem删除结果都在List2中时为true.
delete(?List1, ?Elem, ?List2)
Is true when Lis1, with all occurences of Elem deleted results in List2.
此外,不建议使用delete/3:
also, delete/3 is deprecated:
人们可能想通过多种方式从列表中删除元素以证明其名称合理. 考虑匹配(= vs. ==),先删除/全部删除,确定性与否.
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).
也许?
检查 查看全文