Emacs SLIME Common Lisp Undefined variable

184 Views Asked by At

I'm following Practical Common Lisp Chapter 3 codes.

I've save the following codes to an external files:

(defvar *db* nil)

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))

(defun add-record (cd) (push cd *db*))

(defun dump-db ()
  (dolist (cd *db*)
    (format t "\~{\~a:\~10t\~a\~%\~}\~%" cd)))

I keep getting the following error when I tried to compile it inside Emacs:

compilation unit finished ;
Undefined variable: ;
*DB* ;
caught 1 WARNING condition

Could someone explain to me what is wrong with the code?

What is the different between compiling the above code vs running each line under SLIME?

thanks in advance.

the following is what i tried:

  1. If i try compile all the above codes (cursor at very end), i get the undefined variable warning.
  2. if i try and compile (defvar *db* nil) then rest of the code, I don't get the undefined variable warning (i tried this just before posting, but i was not getting this result in previous attempt, or maybe i did something extra in those previous attempts...). i think i understand why I'm not getting an error with the 2nd method because the *db* is in memory already.
2

There are 2 best solutions below

1
Rainer Joswig On

See the SLIME manual: Compilation Commands

C-c C-c only compiles individual toplevel forms. See the documentation how to compile a region or file.

1
Gwang-Jin Kim On

As the people commented - that was also my suspicion that you didn't run the compiling command for the entire buffer.

To compile the entire file/buffer, do: C-c C-k. Or mark all the code you want by C-S- and for up p, down n, back b, forward f to be run and then call M-x slime-compile-region.