gpt4 book ai didi

list - Prolog 删除 : doesn't delete all elements that unify with Element

转载 作者:行者123 更新时间:2023-12-01 11:03:49 25 4
gpt4 key购买 nike

我对 SWI-Prolog 的 delete/3 谓词有疑问。最简单的方法只是一个简单的例子:

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

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

Delete all members of List1 that simultaneously unify with Elem and unify the result with 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,_),列表)。

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

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

此外,delete/3 已被弃用:

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.

所以最简单的方法就是自己写谓词。像这样的东西:

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

也许吧?

检查 exclude/3, include/3, partition/4

关于list - Prolog 删除 : doesn't delete all elements that unify with Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com