IsaacS
IsaacS

Reputation: 3621

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

Upvotes: 2

Views: 6500

Answers (2)

Talya
Talya

Reputation: 19377

First, note that *count* and count are two different things. Same goes for *resLis* and resLis.

Second, what's that : doing in the middle of func1?

Third, where's anotherFunc?

Fourth, don't use defvar in a function; those are for globals!

Once you work these out, you should find it easier to get going.

Upvotes: 4

Rainer Joswig
Rainer Joswig

Reputation: 139411

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.

Upvotes: 10

Related Questions