Hi Elan.
Russell [EMAIL PROTECTED]
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, December 16, 1999 11:18 PM
Subject: [REBOL] "logical" value referencing ... Re:(7)
> Hi Russell,
>
> You translated the repeated evaluation of the function into repeated
> assignments of a word as a reference to an empty string.
>
> The function:
> >> f: func [/local a] [a: "" insert a 1 a]
> >> f
> == "1"
> >> f
> == "11"
> >> f
> == "111"
>
> does not translate the way you did.
I wasn't translating the function above, but an earlier one in which a and b
were involved and in which
a for loop was used.
But I must admit I'm "flabbergasted" at how the function 'f as defined
immediately above, works on repeated
applications. You proposed some ingenious "debugging" approaches, but I
discovered another. Look at this
console stuff:
>> f: func [/local a] [a: "" insert a 1 a]
>> source f
f: func [/local a][a: "" insert a 1 a]
>> f source f
f: func [/local a][a: "1" insert a 1 a]
>> f source f
f: func [/local a][a: "11" insert a 1 a]
>>
It appears that the interpreter is modifying the source!
>When the expression
> insert a 1
> is evaluated, 1 is inserted into the string referenced by 'a.
>
> Therefore, the second time 'f is evaluated, a is no longer assigned as a
> reference to an empty string. The second time around, 'a is assigned to a
> string that already contains one element, namely the 1 that was inserted
> the first time around. If you want to try that in the REBOL console, you
> have to do the following:
>
> >> a: ""
> == ""
> >> head insert a 1
> == "1"
> >> a: "1"
> == "1"
> >> head insert a 1
> == "11"
> >> a: "11"
> == "11"
> >> head insert a 1
> == "111"
>
The above is certainly correct, as shown by the 'source f results. But I'm
still amazed.
> When you look at the function's code, you may think that the instructions
> above are incorrect. Isn't 'a assigned as a reference to an emtpy string
> each time the function is evaluated? No. A literal string is global and
> therefore the originally empty literal string here retains the 1 that was
> inserted the first time. When the function is evaluated a second time, a
is
> now assigned as a reference to a string that already contains a 1,
> resulting from the first time the function was evaluated.
It surprises me that the interpreter knows that "" does not refer to an
empty string, but to an existing string already referenced by 'a, except in
the first usage whre 'a has no value! I would consider this as a bug, if so
many smart people were'nt
upset about it!
Except for the problem of initialization of 'a for the first execution of
'f, 'f behaves as though it had been defined:
f: func[local/ a][a: a insert a 1 a]
Or as
f: func[local/ a][insert a 1 a]
Try the following varieties:
f: func[local/ a][a: [none] insert a 1 a ]
f: func[local/ a][a: reduce [none] insert a 1 a]
f: func[local/ a][a: reduce "" insert a 1 a]
in the first [none] acts the same as "", but in the second and third [none]
and "" don't give the same result!!
If it walks like a bug and talks like a bug, isn't it a bug?? It disturbs
me that executing f changes the result of
source f
>Look:
>
> >> f: func [/local a] [
> a: ""
> print ["Length of literal string before insert: " length? a]
> insert a 1
> a
> ]
>
> >> f
> Length of literal string before insert: 0
> == "1"
> >> f
> Length of literal string before insert: 1
> == "11"
> >> f
> Length of literal string before insert: 2
> == "111"
> >> f
> Length of literal string before insert: 3
> == "1111"
>
> Another thing you can do is retrieve the string from the function before
it
> is evaluated and after it has been evaluated a few times:
>
> >> f: func [/local a] [a: "" insert a 1 exit]
> >> pick second :f 2
> == ""
> >> f
> >> f
> >> f
> >> f
> >> pick second :f 2
> == "1111"
>
> See, after evaluating f a few times, the string IN THE function is "1111".
>
> Elan
>
I agree; that disturbs me!