[REBOL] [Function] with [Variable number of args]
From: gerardcote:sympatico:ca at: 5-Apr-2004 17:36
while relaxing for a couple of days, I reread one of my old APL books and wanted to try
implement some useful mechanisms for vector
operations (a vector being a one dimensional array) using simple REBOL scripts. some
of these mechanisms are somewhat similar to the
higher-level functions Ladislav and other advanced REBOLers have written in the past.
I'll give you 2 simple examples below since
this is where I found some questions about the way REBOL can or not manage functions
with a variable number of arguments.
One of the mechanisms I wanted to emulate is simply an extension of the maths operators
to take into account two arguments that are
not only single values but that also could be a block of values, like in the use of my
++ function :
>>++ 1 2
>>++ 1 [1 2 3 4 5]
==[2 3 4 5 6]
>>++ [6 7 8 9 10] 1
==[7 8 9 10 11]
>>++ [1 2 3] [4 5 6]
==[5 7 9]
All of this works well for now.
The problem is that I also wanted to include in the same function another useful behaviour
generally known as +/ in APL, like in
this example :
>>++ [1 2 3 4] ; 1 + 2 + 3 + 4 == 10
But I never was able to say REBOL to use only one argument - leaving the second one optional
when necessary - and to not take into
account of the second one defined like this :
++: function [arg1 arg2][
Can someone tell me if there is one simple way to do this with REBOL or if I have to
define another independant function to do this.
For now what I can do is check if the second arg is an empty block or none but this is
not as elegant as it could be.
I must admit that I didn't begin to look for an answer by myself yet but I will do this
tonight. I found this could be interesting
for other newbies too to know more about the subject.