Вопрос

When I use setf in the repl it works fine, but if I try to use it in a file and then compile it and send it to the repl, it gives an error. Why am I seeing this strange behavior?

Works fine in repl:

(setf books 
  '((war-and-peace leo-tolstoy)
(spin charles-wilson)
(harry-potter jk-rowling)
(speaker-for-the-dead orson-scott-card)))

Gives this error when compiled from file:

clagitsc.lisp:187:1:
warning: 
BOOKS is neither declared nor bound,
it will be treated as if it were declared SPECIAL.
warning: 
in #:|187 193 (SETF BOOKS '(# # # ...))-34| in lines 187..193 : BOOKS is neither    
declared nor bound,
it will be treated as if it were declared SPECIAL.

Compilation failed.

I'm using slime with emacs 24.3 on windows with CLisp 2.49 and I'm working my way through Common Lisp: A Gentle Introduction to Symbolic Computation.

Это было полезно?

Решение

That is because books is not explicitly declared as a variable. In repl mode your lisp maybe silently assumes it is special but during compilation it throws a warning. You can 'create' global dynamic variable using defvar or defparameter forms. (Read more here: http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/mac_defparametercm_defvar.html). To create C-like globals (that are just globals i.e. share value between all threads) you should find analog for sb-ext:defglobal in CLisp.

Другие советы

The exact effects of setting an undeclared variable is undefined in Common Lisp. In CLISP you get thus a warning. But a warning is not an error and compilation usually does not fail in CLISP because of that.

Nowadays we write it as

(defvar *books* '(...))

and use the variable *books* to make it clear that it is a variable which is declared special, globally. It prevents clashes with lexical variables.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top