Hello [EMAIL PROTECTED]!

On 08-Gen-00, you wrote:

 w>   1. Can you simulate a static local variable in REBOL? The

There are plenty of ways to do this...

>> use [static] [            
[    static: none
[    f: func [a] [             
[        print ["Last was:" static]
[        static: a                 
[        ]
[    ]
>> f 1
Last was: none
== 1
>> f 5
Last was: 1
== 5
>> f "But..."
Last was: 5
== "But..."
>> recycle
>> f "... there's still the bug!"
** CRASH (Should not happen) - Corrupt datatype: 66

Just another one:

>> f: func [a] [
[    static: [none]
[    print ["Last was:" static/1]
[    change/only static a
[    ]
>> f 1
Last was: none
== []
>> f 4
Last was: 1
== []
>> f "This should work always, I hope..."
Last was: 4
== []
>> f ":-)"
Last was: This should work always, I hope...
== []

 w>   2. Can you create a line of REBOL code, including two or
 w> more functions of arity one or more (that take one or more
 w> parameters), that results in the same result forwards or
 w> backwards? To verify, put the code in a block, and try:

Well, this way's too simple. :-) I could write:

>> f: func [:a :b] ["Result"]
>> code: [f f f]
== [f f f]
>> do code
== "Result"
>> do head reverse code
== "Result"

It's better to exclude these simple cases from the puzzle, to make
it more interesting... :-)

 w>   3. Can you make a function which takes one argument, unless
 w> called with a refinement in which case the function takes no
 w> arguments?

This one's challenging, at least at first sight. One solution
might be:

>> f: func [      
[    [catch]
[    a [any-type!]  
[    /b             
[    ] [
[    either b [     
[        "No parameter!"
[        ] [
[        if not value? 'a [throw make error! [script no-arg f a]]
[        "One parameter"
[        ]
[    ]
>> f
** Script Error: f is missing its a argument.
** Where: f
>> f 2
== "One parameter"
>> f 4
== "One parameter"
>> f/b
== "No parameter!"

But:

>> f/b 4
== "No parameter!"

Hmm... I'll have to think about this... :-)

 w>   4. Can you create a parse rule that recognizes whether a
 w> string of numbers is from the Fibonacci sequence?

Generalizing for any sequence,

>> sequence: [13 46 38 59 79 47 22 4 68 2]
== [13 46 38 59 79 47 22 4 68 2]
>> string: "  13  46 38   59  79 47 22    4 68   2"
== "  13  46 38   59  79 47 22    4 68   2"
>> parse string [                                  
[    (i: 1 number: form sequence/1)           
[    some [                                   
[        number (i: i + 1 number: form pick sequence i)
[        ]
[    ]
== true

You can apply the same kind of rule for any function...

Regards,
    Gabriele.
-- 
o--------------------) .-^-. (----------------------------------o
| Gabriele Santilli / /_/_\_\ \ Amiga Group Italia --- L'Aquila |
| GIESSE on IRC     \ \-\_/-/ /  http://www.amyresource.it/AGI/ |
o--------------------) `-v-' (----------------------------------o

Reply via email to