我正在测试 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/