[EMAIL PROTECTED] wrote:
> 
>    1. Can you simulate a static local variable in REBOL?  The variable
> should be local to a function, but each time the function is called,
> the variable has the same value it had at the end of the last function
> call.  The variable should be modified in some way in the function
> (incremented for instance) and it should have the modified value on
> subsequent calls.  I have one solution, but I think there could be
> others.
> 

Here's one approach... nothing subtle, but it works.  (First line
reformatted for readability, but transcript otherwise unmodified.)

    >> incr: func [/set-to n /local _a a] [
           _a: [0]
           either set-to [a: n] [a: 1 + first _a]
           change _a a
           a
       ]
    >> incr
    == 1
    >> incr
    == 2
    >> incr
    == 3
    >> incr/set-to 17
    == 17
    >> incr
    == 18
    >> incr
    == 19

And a slight variation which harks back to a long-running thread ;-)

    >> grow: func [/reset /local a] [a: "" if reset [clear a] append a
"*" a]
    >> grow
    == "*"
    >> grow
    == "**"
    >> grow
    == "***"
    >> grow
    == "****"
    >> grow/reset
    == "*"
    >> grow
    == "**"

Still pondering the others...

-jn-

Reply via email to