[EMAIL PROTECTED] wrote:
> 
> Ex2:
> a: [""]
> b: copy a
> change/only a "x"
> a
> b
> 
> >> a: [""]
> == [""]
> >> b: copy a
> == [""]
> >> change/only a "x"
> == []
> >> a
> == ["x"]
> >> b
> == [""]
> You see, that by changing A we didn't change it's copy B.
> 
> Ex3:
> a: [""]
> b: copy a
> insert first a "x"
> a
> b
> 
> Results:
> >> a: [""]
> == [""]
> >> b: copy a
> == [""]
> >> insert first a "x"
> == ""
> >> a
> == ["x"]
> >> b
> == ["x"]
> 
> Now, even B looks changed. How did I manage to do it?
> 

Because you used 'copy without the /deep refinement.  The difference
is that 'copy by default only copies the sequence of its argument
series and returns a series referencing that sequence.  After the
first two lines of Ex3, we can model the state of affairs as:

    a -> {{block! 1 *}}
                    |
                    V
                    << * >>
                       |
                       V
                       {{string! 1 *}}
                       ^           |
                       |           V
                       |           <<>>
                    << * >>
                    ^
                    |
    b -> {{block! 1 *}}

The sequence under a's value is a single string reference.  The
sequence under b's value is a copy, so it refers to the same string.
The expression

    first a

evaluates to another reference to that same string, which is passed
to 'insert.  Modifying that string (via any reference to it) has the
side effect on ALL references to the same string.

    >> a: [""]
    == [""]
    >> b: copy a
    == [""]
    >> same? a b
    == false

Because they're different blocks, but...

    >> same? first a first b
    == true

Becuase both blocks refer to the same string.

    >> append first a "now you see it!"
    == "now you see it!"
    >> a
    == ["now you see it!"]
    >> b
    == ["now you see it!"]

Now, using deep copying...

    >> a: [""]
    == [""]
    >> b: copy/deep a
    == [""]
    >> same? a b
    == false
    >> same? first a first b
    == false

A different block, referring to a different string.

    >> append first a "now you don't!"
    == "now you don't!"
    >> a
    == ["now you don't!"]
    >> b
    == [""]

-jn-

Reply via email to