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?
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With