Structural Informatics Group (SIG) logo
Home | Projects | Demos | Downloads | Publications | Local Info | About Us | New site
Go to the first, previous, next, last section, table of contents.

&rest

&rest

type: keyword
location: built-in
source file: xleval.c
Common LISP compatible: yes
supported on: all machines

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 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


Go to the first, previous, next, last section, table of contents.