gpt4 book ai didi

lisp - #'adjoin in Common Lisp work as per HyperSpec when used with ` :key`?

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:13 25 4
gpt4 key购买 nike

查看 docs for #'adjoin在 HyperSpec 中,我在示例部分看到以下内容:

(setq slist '()) =>  NIL
(setq slist (adjoin '(test-item 1) slist)) => ((TEST-ITEM 1))
(adjoin '(new-test-item 1) slist :key #'cadr) => ((TEST-ITEM 1))

我本来期望的是:

(adjoin '(new-test-item 1) slist :key #'cadr) =>  ((NEW-TEST-ITEM 1) (TEST-ITEM 1))

我的期望是由于 HyperSpec (17.2.1) 中的以下文本:

When an object O is being considered iteratively against each element Ei of a sequence S by an operator F listed in the next figure, it is sometimes useful to control the way in which the presence of O is tested in S is tested by F. This control is offered on the basis of a function designated with either a :test or :test-not argument.

并且,进一步:

The object O might not be compared directly to Ei. If a :key argument is provided, it is a designator for a function of one argument to be called with each Ei as an argument, and yielding an object Zi to be used for comparison. (If there is no :key argument, Zi is Ei.)

The function designated by the :key argument is never called on O itself. However, if the function operates on multiple sequences (e.g., as happens in set-difference), O will be the result of calling the :key function on an element of the other sequence.

所以我们有 slist(序列,S)作为 '((TEST-ITEM 1)) 和 O 作为 '(new-test-item 1)。为了检查 O 是否应该连接,将函数 #'cadr 应用于 S 的元素,第一个元素是 '(test-item 1)。所以,该测试给出:

(cadr '(test-item 1)) => 1

现在,当 O, '(new-test-item 1) 被检查为将 #'cadr 应用于 S 的 E1 的结果时 #'eql(:test 提供 none 时使用的相等函数),false 结果应该意味着 O 是邻接的。至少那是我的想法。我误会了什么?

最佳答案

这是 HyperSpec 和 Issue ADJOIN-SPECIFICATION 中的错误已经在 CLiki 上写过。相关部分是:

Problem Description:

CLHS specifies ADJOIN behavior in presence of a :KEY argument by reference to Section 17.2.1 Satisfying a Two-Argument Test. This is incorrect, since section 17.2.1 specifies that the key function is NOT called on the ITEM parameter, but ADJOIN does, as is specified in pushnew. Proposal (ADJOIN:CLARIFICATION):

Replace:

The test, test-not, and key affect how it is determined whether item is the same as an element of list. For details, see Section 17.2.1 (Satisfying a Two-Argument Test).

通过:

Whether or not item is already a member of the list is determined by comparisons using :test or :test-not. The first argument to the :test or :test-not function is the result returned by the :key function (if supplied), applied to the item; the second argument is an element of the list as returned by the :key function (if supplied). If :key is supplied, it is used to extract the part to be tested from both item and the list element.

理由:

阐明 ADJOIN 的规范。

目前的做法:

所有实现都按照在 pushnew 上指定的方式实现 ADJOIN,而不是在 adjoin 上指定。

关于lisp - #'adjoin in Common Lisp work as per HyperSpec when used with ` :key`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22359337/

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