LISP - Optional Parameters



You can define a function with optional parameters. To do this you need to put the symbol &optional before the names of the optional parameters.

Let us write a function that would just display the parameters it received.

Example

Create a new source code file named main.lisp and type the following code in it.

(defun show-members (a b &optional c d) (write (list a b c d)))
(show-members 1 2 3)
(terpri)
(show-members 'a 'b 'c 'd)
(terpri)
(show-members 'a 'b)
(terpri)
(show-members 1 2 3 4)

When you execute the code, it returns the following result −

(1 2 3 NIL)
(A B C D)
(A B NIL NIL)
(1 2 3 4)

Please note that the parameter c and d are the optional parameters in the above example.

lisp_functions.htm
Advertisements