XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

&rest


Type:   -   keyword
Source:   -   xleval.c

Syntax

&rest [rest-arg]
rest-arg - rest argument symbol

Description

In XLISP, there are several times that you define a formal argument list for a body of code like defun , defmacro , :answer and lambda. All of the formal arguments that are defined are required to appear in the invocation of the defined function or operation. If there are any &optional arguments defined, they will be filled in order. If there is a '&rest' argument defined, and all the required formal arguments and &optional arguments are filled, any and all further parameters will be passed into the function via the 'rarg' argument. There can be only one 'rest-arg' argument for '&rest'. If there are insufficient parameters for any of the &optional or '&rest' arguments, they will contain NIL. At the end of the function or operation execution, these local symbols and their values are removed.

Examples

(defun foo                             ; define function FOO
  (a b &optional c d &rest e)          ;   with some of each argument
  (print a) (print b)
  (print c) (print d)                  ;   print out each
  (print e))
(foo)                                  ; error: too few arguments
(foo 1)                                ; error: too few arguments
(foo 1 2)                              ; prints 1 2 NIL NIL NIL
(foo 1 2 3)                            ; prints 1 2 3 NIL NIL
(foo 1 2 3 4)                          ; prints 1 2 3 4 NIL
(foo 1 2 3 4 5)                        ; prints 1 2 3 4 (5)
(foo 1 2 3 4 5 6 7 8 9)                ; prints 1 2 3 4 (5 6 7 8 9)

(defun my-add                          ; define function MY-ADD
  (num1 &rest num-list &aux sum)       ;   with 1 arg, rest, 1 aux var
  (setq sum num1)                      ;   clear SUM
  (dotimes (i (length num-list) )      ;   loop through rest list
    (setq sum (+ sum (car num-list)))  ;      add the number to sum
    (setq num-list (cdr num-list)))    ;      and remove num from list
  sum)                                 ;   return sum when finished
(my-add 1 2 3 4)                       ; returns 10
(my-add 5 5 5 5 5)                     ; returns 25

See the &rest keyword in the XLISP 2.0 manual.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next