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. 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"

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. 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

Reply via email to