Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: variable number of arguments

From: lmecir:mbox:vol:cz at: 29-Nov-2000 9:47

Andrew Martin, AKA Rebol assembler! wrote: It's been interesting. I had to really think about it. Reminded me of programming in Intel 8085 machine code and Motorola 6809 assembler, but this was a lot easier. Envying Andrew I dare to write a lengthy essay on the subject. Task #1: Write a Rebol function Eval-1 taking a Rebol function as its first argument. Eval-1 shall check its first argument to make sure, that it is a one-argument-taking Rebol function. Next it shall evaluate it. The number of Eval-1's arguments shall be as low as possible to accomplish the requirements above. The first argument shall be passed normally (i.e. neither unevaluated nor fetched). My Eval-1 implementation is below The Line. (Don't look there, if you want to try to do it on your own first.) Task #2: Write a Rebol function Eval-2 taking a Rebol function as its first argument. Eval-2 shall check its first argument to make sure, that it is a one-argument-or-two-arguments-taking Rebol function. Next, it shall evaluate it. The first argument shall be passed normally (i.e. neither unevaluated nor fetched), moreover, you can suppose, that it uses the normal argument passing too. The number of Eval-2's arguments shall be as low as possible. Again, my implementation is below The Line. The Line -------------------------------------------------------------------------- ; uncomment the line below, if you don't have defined the Nargs function before ;do http://www.sweb.cz/LMecir/highfun.r eval-1: func [ [catch] function [any-function!] arg [any-type!] ] [ if (nargs :function) <> 1 [ throw make error! {Incorrect function!} ] function get/any 'arg ] Let's test it: f1: func [x] [x] f2: func [x [any-type!]] [type? get/any 'x] f3: does [1]
>> eval-1 :f1 1
== 1
>> eval-1 :f2 :f1
== function!
>> eval-1 :f3
** User Error: Incorrect function! ** Near: eval-1 :f3 eval-2: func [ [catch] function [any-function!] args [block!] ] [ if all [ (nargs :function) <> 1 (nargs :function) <> 2 ] [ throw make error! {Incorrect function!} ] do append copy [function] args ] Well, let's have a look: f4: func [x y] [reduce [x y]] eval-2 :f1 [1] eval-2 :f2 [:f1] eval-2 :f3 [1 2] eval-2 :f4 [1 2] (tried your implementation?) When I wrote my Eval-2's implementation, I was told, that its interface was ugly and that the interface of the Do native looked much better, as in: do :f4 1 2 That really looks better. I realized, that the only way, how to mimic the behaviour of the Do native is to allow Eval-2 to take three arguments or two arguments, depending on the Function argument, which is something I shall describe in the next part. To be continued... Ladislav