Lisp SYMBOL-PACKAGE-LOCKED-ERROR

微笑、不失礼 提交于 2019-12-20 05:23:14

问题


I'm new to Lisp so when I wrote the function in SBCL

(defun subst (new old l)
  (cond   
   ((null l) '())
   ((eq old (car l)) (cons new (cdr l)))
   ((cons (car l) (subst new old (cdr l))))))

it gives error SYMBOL-PACKAGE-LOCKED-ERROR,a Style-Warning and a Warning, please help to resolve it


回答1:


You're trying to redefine cl:subst. According to §11.1.2.1.2 of the HyperSpec, it's undefined what happens when you try to do that. Most implementations have some sort of package lock which prevents such redefinitions. You can get around those, by unlocking the package, but it would be better in this case to either use a name other than subst (e.g., my-subst), or to define a new package, say my-cl, that shadows cl:subst and define my-cl:subst instead.

The error that SBCL gives is actually rather informative and provides a reference to the HyperSpec page that I linked to above, as well as the Chapter 11. Package Locks from the SBCL manual:

* (defun subst (new old l) 
    (cond
      ((null l) '())
      ((eq old (car l)) (cons new (cdr l)))
      ((cons (car l) (subst new old (cdr l))))))
; in: DEFUN SUBST
;     (SB-INT:NAMED-LAMBDA SUBST
;         (NEW OLD L)
;       (BLOCK SUBST (COND ((NULL L) 'NIL) ((EQ OLD #) (CONS NEW #)) ((CONS # #)))))
; ==>
;   #'(SB-INT:NAMED-LAMBDA SUBST
;         (NEW OLD L)
;       (BLOCK SUBST
;         (COND ((NULL L) 'NIL) ((EQ OLD #) (CONS NEW #)) ((CONS # #)))))
; 
; caught STYLE-WARNING:
;   The definition has no &KEY arguments, but the proclamation did.

;     (SUBST NEW OLD (CDR L))
; 
; caught WARNING:
;   recursion in known function definition
;     policy=((COMPILATION-SPEED . 1) (DEBUG . 1) (INHIBIT-WARNINGS . 1)
;             (SAFETY . 1) (SPACE . 1) (SPEED . 1))
;     arg types=(T T T)
; 
; compilation unit finished
;   caught 1 WARNING condition
;   caught 1 STYLE-WARNING condition
STYLE-WARNING: redefining COMMON-LISP:SUBST in DEFUN

debugger invoked on a SYMBOL-PACKAGE-LOCKED-ERROR in thread #<THREAD
                                                              "initial thread" RUNNING
                                                               {1002978E71}>:
  Lock on package COMMON-LISP violated when setting fdefinition of SUBST while
  in package COMMON-LISP-USER.
See also:
  The SBCL Manual, Node "Package Locks"
  The ANSI Standard, Section 11.1.2.1.2


来源:https://stackoverflow.com/questions/16301917/lisp-symbol-package-locked-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!