2
votes

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

2
what is the colon doing there? - Rainer Joswig
As I mentioned in the comment to @Arlen Cuss' answer, I just tried to simplify, thinking that part wasn't critical to the question. Since 2 persons wondered, I modified my question to add the hidden part. - IsaacS

2 Answers

10
votes

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.

4
votes

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.