[REBOL] Re: variable number of arguments
From: lmecir:mbox:vol:cz at: 26-Nov-2000 12:09
> Ladislav Mecir wrote:
> > Andrew Martin wrote:
> > > Graham Chiu wrote:
> > > > Can a function have a variable number of arguments?
> > > No. But you can simulate it, by using 'any-type! function specifiers
> passing unset! as arguments. Better is to use refinements.
> > Yes, a function can have a variable number of arguments. Do is such a
> function, as e.g. in:
take-n: func [n /local spec] [
spec: copy 
for i 1 n 1 [
append spec to word! append copy "a" to string! i
func spec reduce [:reduce append reduce [to lit-word! append copy "take"
to string! n] spec]
> > do take-n 4 1 2 3 4
> > == [take4 1 2 3 4]
> > , where Do took 5 arguments.
> Actually, 'do took only one argument, the value returned from 'take-n
> had the argument "4"), which happened to be a function that is evaluated
> 'do that could take the next four arguments "1 2 3 4".
Think for the second time, please. Let's make sure, how many arguments we
have to take:
make-sure1: func [
This function takes just one argument
; evaluate the argument
make-sure1 take-n 4 1 2 3 4
** Script Error: f is missing its a1 argument
** Where: make-sure1
** Near: f
make-sure2: func [
This function takes five arguments
f a2 a3 a4 a5
; evaluate f
f a2 a3 a4 a5
make-sure2 take-n 4 1 2 3 4
== [take4 1 2 3 4]
Conclusion: Do had to take five arguments to yield the correct result.