lisp - 这是文字缺点的特定于实现的行为吗?

标签 lisp common-lisp sbcl

我正在测试 this interesting answer 中的代码.

CL-USER> (defun literal-cons ()
        (let ((cons '(1 . 2)))
          (incf (cdr cons))
          cons))
; in: DEFUN LITERAL-CONS
;     (INCF (CDR CONS))
; --> LET* 
; ==>
;   (SB-KERNEL:%RPLACD #:CONS1 #:NEW0)
; 
; caught WARNING:
;   Destructive function SB-KERNEL:%RPLACD called on constant data.
;   See also:
;     The ANSI Standard, Special Operator QUOTE
;     The ANSI Standard, Section 3.2.2.3
; 
; compilation unit finished
;   caught 1 WARNING condition
LITERAL-CONS
CL-USER> (literal-cons)
(1 . 3)
CL-USER> (literal-cons)
(1 . 3)
CL-USER> (literal-cons)
(1 . 3)

由于行为不一样,我想知道 SBCL 是否使用上述警告将行为更改为它认为用户更有可能期望的行为?预期:

TEST> (defun literal-cons ()
        (let ((cons '(1 . 2)))
          (incf (cdr cons))
          cons))
LITERAL-CONS
TEST> (literal-cons)
(1 . 3)
TEST> (literal-cons)
(1 . 4)
TEST> (literal-cons)
(1 . 5)

最佳答案

简短的回答是,是的,这是特定于实现的行为。正如在 Unexpected persistence of data 中讨论的那样,

The relevant text from the HyperSpec on quote is:

The consequences are undefined if literal objects (including quoted objects) are destructively modified.

这意味着您从此类函数中看到的任何行为都是特定于实现的(即使某些行为在实现中比其他行为更常见)。

关于lisp - 这是文字缺点的特定于实现的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342793/

相关文章:

closures - mapcan,尖锐的报价和关闭

sockets - SBCL 套接字 : reuse-address

lisp - 普通口齿不清 : How to quote parenthese in SBCL

functional-programming - 在Scheme中,如何理解 "(define (f x . y) (cons x y))"

lisp - Lisp 中余弦函数的泰勒级数

scheme - 给定索引列表重新排列列表

list - 替换 Common Lisp 列表中的项目?

java - 斯坦福解析器

package - UIOP 无法识别本地昵称关键字

scope - lisp 的 SBCL 是否以不同方式处理范围?它似乎没有将范围传递给被调用的函数?