Pergunta

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

Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top