LISP – Rest Parameters

  • Post author:
  • Post category:LISP
  • Post comments:1 Comment
LISP - Rest Parameters

This topic is about LISP – Rest Parameters.

Some functions need to take a variable number of arguments.

For example, the format function we are using needs two required arguments, the stream and the control string. However, after the string, it needs a variable number of arguments depending upon the number of values to be displayed in the string.

Similarly, the + function, or the * function may also take a variable number of arguments.

You can provide for such variable number of parameters using the symbol &rest.

The following example illustrates the concept −

Example

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

(defun show-members (a b &rest values) (write (list a b values)))
(show-members 1 2 3)
(terpri)
(show-members 'a 'b 'c 'd)
(terpri)
(show-members 'a 'b)
(terpri)
(show-members 1 2 3 4)
(terpri)
(show-members 1 2 3 4 5 6 7 8 9)

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

(1 2 (3))
(A B (C D))
(A B NIL)
(1 2 (3 4))
(1 2 (3 4 5 6 7 8 9))

In this topic we learned about LISP – Rest Parameters. To know more, Click Here.

This Post Has One Comment

Leave a Reply