Lisp function with default argument value
Asked Answered
P

1

9

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?

Pelagias answered 1/5, 2015 at 16:28 Comment(2)
What's the purpose of a default, if the parameter is not optional?Rycca
Every positional argument with a default has to be optional or else the default would never be used.Capitalism
T
21

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.

Thaothapa answered 1/5, 2015 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.