Search code examples
common-lispsbclclisp

Undefined variable on sbcl, not on clisp


Loading the following 2 functions on clisp goes successful.

(defun func1 (l)
  (defvar *count* nil)
  (setq count 1)
  (cond ((null l) 0)
        ((atom l) (+ count 1))
        ((atom (car l)) (+ count (func1 (cdr l))))
        ((listp (car l)) (+ (func1 (car l)) (func1 (cdr l))))
        (t nil))    )
(defun func2 (l)
  (defvar *resLis* nil)
  (setq resLis '((0 0)))
  (anotherFunc l resLis)  
)

However, sbcl causes error:

warning: undefined variable: COUNT
warning: undefined variable: RESLIS
Compilation failed.

I prefer using sbcl (since my slime only goes well with it) but what's wrong with the codes above?

Environment: Ubuntu 11.10, GNU CLISP 2.49, SBCL 1.0.50.0.debian


Solution

  • SBCL does not 'causes error'. The compiler prints a warning. CLISP may not warn, if you use its interpreter and not its compiler. SBCL uses the compiler by default.

    What's wrong?

    • DEFVAR is a top-level form defining a global variable. Using it in a function is possible, but not recommended.

    • count is simply undefined. As SBCL says. You have nowhere a variable count defined.