variable length argument lists

Pondering the philosophy behind the language
Locked
jsmall
Posts: 26
Joined: Mon Sep 20, 2004 1:44 am

variable length argument lists

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked