Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp function with default argument value

Tags:

common-lisp

I would like to have a CL function with a single argument, but also with a default argument value.

(defun test1 ((x 0))
  (+ x x))

would seem to be the syntax, but it doesn't work. The tutorials I'm seeing have the argument-default form like above only in use with &optional and &key. Is it possible to have just one function argument and it with a default?

like image 245
147pm Avatar asked May 01 '15 16:05

147pm


People also ask

What is &optional in Lisp?

We can elegantly define procedures that include optional parameters in LISP by using the &OPTIONAL keyword in the argument list of a function definition. By enclosing the parameter(s) that follow the &optional keyword in parentheses one can supply a default value (otherwise they are automatically defaulted to nil).

What is default argument function?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

What are functions in Lisp?

Advertisements. A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.

What is let in Lisp?

The let expression is a special form in Lisp that you will need to use in most function definitions. let is used to attach or bind a symbol to a value in such a way that the Lisp interpreter will not confuse the variable with a variable of the same name that is not part of the function.


1 Answers

You need to signal that it is an optional argument:

(defun test1 (&optional (x 0))
   (+ x x))

As written, you have specified an invalid lambda list and should hopefully have seen some diagnostics from the REPL.

like image 185
Vatine Avatar answered Oct 10 '22 14:10

Vatine