1

I'm having some problem writing a LISP function. The function is defined as

(defun foo (arg1 &optional cont)) (cond ((null arg1) nil) ((= 0 cont) arg1) ((do_something)) ((recursive call)))) 

When i call the function with cont everything works fine, but when I call it just with arg1 the error returned is:

Error: in ZEROP of (NIL) arguments should be of type NUMBER 

I guess something is wrong in the condition ((= 0 cont) arg1), can you help me solve this problem? Thanks

2
  • 1
    If you want to know if an optional argument is provided, you can do that: (defun foo (arg1 &optional (cont nil contp)) ... for instance Commented Feb 1, 2017 at 17:38
  • 1
    Btw., the error message seems to indicate that the call to = has been replaced by a call to zerop by the compiler. Commented Feb 1, 2017 at 19:56

1 Answer 1

7

The = function, along with some other ones, expect exclusively numbers.

You need to use EQL or a more general equality comparison (equal, equalp) when you expect NIL to be a valid input; here, NIL is expected because it is the default value of the optional argument.

You could also provide a numerical default value to cont:

 ... &optional (cont 0) ... 

... which might be the correct approach if cont has no reason to be anything else than a number.

Sign up to request clarification or add additional context in comments.

1 Comment

Alternative: the condition could be (and (numberp cont) (= 0 cont)) if cont might have a reason not to be a number.