Page 1 of 1

variable length argument lists

Posted: Wed Sep 22, 2004 2:12 am
by jsmall
Without resorting to define-macro and (args) what
is the preferred idiom for specifying a function
taking a variable number of arguments?

Do you simply have to anticipate these optional arguments?

(define (foo x y z) ...)

But if this is the case than passing this variable number
of arguments onto another such function such as a native
function is problematic.

(define (foo x y z) (+ x y z)) ;; no good if y or z is nil

Do I have to resort to prepacking my arguments into an array.

(define (foo x optargs-list)
(apply + (cons x optargs-list)))

Posted: Thu Sep 23, 2004 1:00 am
by Lutz
What I do, is anticipate the number of vars i.e. (foo x y z) then I test i.e. (if y (...) (...)) etc. Variables which are not used will contain nil.

Sometimes I set those variables to a default i.e. (if (not y) (set 'y 0)) where '0' would be the default value.

In newLISP when you pass too much args they get simply ignored.

Lutz