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?

3
  • 1
    // is a valid token. In particular, it does not introduce a comment. Comments are introduced with ; or included in #| |#. Commented Jun 5, 2012 at 22:19
  • You are right. I added these "//" only in my question.. Commented Jun 6, 2012 at 6:52
  • Btw., 'CL-USER 13 : 4 >' means that you are in a debug level. You might want to go back to the top-level. Use the command :top . Commented Jun 6, 2012 at 7:48

1 Answer 1

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..

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

4 Comments

impressive.. you can be a poem writer.. now I see my error..I had to write only n...thanks dude
Unfortunately, it still gives the same error also after I changed every ocurrence of "(n)" to n..what is the problem now? thanks
@זאבי כהן: then add this new version of your code to your question and we will look again.
@זאבי כהן: I don't see a problem in the changed code. How does the error occur?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.