[REBOL] Re: variable number of arguments
From: lmecir:mbox:vol:cz at: 30-Nov-2000 21:25
For many people the meaning of:
The function took N arguments.
may be unclear. To be as precise as possible, here is my definition:
args-taken?: function [
{How many arguments a function took?}
f [any-function!]
args [block!]
] [test] [
test: make block! 1 + length? args
append test :f
repeat i length? args [
append/only test to paren! reduce [
:pick 'args i
]
]
test: do/next test
if empty? second test [
print {Warning! To obtain the complete result you should supply more
arguments.}
]
(length? args) - (length? second test)
]
Let's test the above definition:
>> args-taken? :list-dir []
feedback.r license.html nntp.r notes.html rebdoc.r
rebol.exe rebol.r setup.html user.r
Warning! To obtain the complete result you should supply more arguments.
== 0
Here the List-dir function took 0 arguments, but it might have done so just
because we didn't supply more. To make sure, we will watch what happens, if
we supply more arguments:
>> args-taken? :list-dir [%.]
feedback.r license.html nntp.r notes.html rebdoc.r
rebol.exe rebol.r setup.html user.r
Warning! To obtain the complete result you should supply more arguments.
== 1
>> args-taken? :list-dir [%. %.]
feedback.r license.html nntp.r notes.html rebdoc.r
rebol.exe rebol.r setup.html user.r
== 1
Now a more complicated example:
>> args-taken? :- [1 2 3]
== 1
The above result is telling us, that the - op used as a prefix operator
normally takes just one argument, even though Rebol is saying:
>> help -
USAGE:
value1 - value2
DESCRIPTION:
Returns the second value subtracted from the first.
- is an op value.
ARGUMENTS:
value1 -- (Type: number pair char money date time tuple)
value2 -- (Type: number pair char money date time tuple)
>> args-taken? :do [:- 1 2 3]
== 1
Here we see, what the majority expected: Do took one argument.
>> args-taken? :do reduce [:- 1 2 3]
== 3
Wow!
Ladislav (preferring Rebol to assembler)