(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..
//is a valid token. In particular, it does not introduce a comment. Comments are introduced with;or included in#||#.