0

I have this function in LISP with regular parameter and optional paremater n:

(defun lastplus (x &optional (n 0)) //default value for n is 0
    ( if (listp x) //if x is a list
        (
            (list (length x) (n)) //return list that contains length(x) and n
        )
        (n) //else return n
    )
)

I am trying to use the function in the listener file but it gives me this error:

CL-USER 13 : 4 > (lastplus 2 8) 

Error: Undefined function N called with arguments ().

I use LispWorks 6.0.1

Do you know why do I get this error?

זאבי כהן
  • 317
  • 6
  • 16

1 Answers1

11
(defun lastplus (x &optional (n 0)) //default value for n is 0
    ( if (listp x) //if x is a list
        (
            (list (length x) (n)) //return list that contains length(x) and n
        )
        (n) //else return n
    )
)

Your formatting style is not Lispy.

Adapt to Lisp formatting:

(defun lastplus (x &optional (n 0)) ; default value for n is 0
   (if (listp x) ; if x is a list
        ((list (length x) (n))) ; return list that contains length(x) and n
     (n)))

You said: cannot call function with optional parameter.

Sure you can. The error message did say something else. You can call a function with an optional parameter. The error is inside the function.

The error says: Error: Undefined function N called with arguments ().

So you are calling a function called N, which does not exist. With no arguments. Like in (n). Check your code - can you find (n)?

Now ask yourself:

  • What does a function call look like?

  • Answer: open parenthesis, function, possibly some arguments, closing parenthesis

  • What does (n) look like?

  • Answer: it looks like a function call.

  • Is that what you wanted?

  • Certainly not.

  • What did you want?

  • The variable value.

  • What does that look like?

  • just n.

  • Are there other errors?

  • Hmm.

  • What about the form on the third line?

  • That looks wrong, too.

  • It's wrong, too. Same error..

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346