tags:

views:

33

answers:

2

http://www.rebol.org/ml-display-thread.r?m=rmlJNWS

Graham wrote:

Can a function have a variable number of arguments? No. But you can simulate it, by using 'any-type! function specifiers and passing unset! as arguments. Better is to use refinements.

+1  A: 

The answer on that page is:

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]
Rowland Shaw
take-n returns a block so this is one argument. In fact I didn't ask the right question, I rather want to be able to write something liketweet Hello World instead of tweet "Hello World".Finally I found a way.
Rebol Tutorial
+1  A: 

Rebol's default dialect (the do dialect) does not support the notion of a call to a function having a variable number of arguments. If you want to break a rule as fundamental as this, then you need your own dialect. Nothing stopping you from making:

tweet [Hello World How Are You Today?]

But the idea of using word! instead of string! in this case is a little dodgy, as many common tweets aren't valid for the Rebol parser:

tweet [LOL! :)]

Neglecting that issue, note that by default you won't get any expression evaluation. So this tweet dialect would have to choose a way to show where you want an evaluation. You might use get-word elements to do variable substitution, and parentheses for more general evaluations:

>> a: 10
>> b: 20
>> tweet [When you add :a and :b you get (a + b), LOL ":)"]
"When you add 10 and 20 you get 30, LOL :)"

BTW, take-n in Rowland's example isn't returning a block. Not directly, I mean. It's perhaps better understood with parentheses, and by addressing the implicit "do" that's there on every interpretation:

do [do (take-n 4) 1 2 3 4]

take-n works with only one parameter (the "n") and then returns a function which takes n parameters. Let's call that function f, so step one of this evaluation turns into something equivalent to:

do [f 1 2 3 4]

When the second do kicks in, that function gets run...and it's returning a block. In practice, I doubt you're going to want to be counting the parameters like this.

Hostile Fork
Thanks a lot hostilefork your answers are always thoughtfull, I will have a long week-end to read all of them thorougly :)
Rebol Tutorial