[REBOL] Re: reduce/deep
From: maximo:meteorstudios at: 22-Oct-2003 9:41
sorry if I come in late in this discussion, I didn't have time to properly follow it.
so it could be that what I post here is redundant to some previous post, but maybe not...
quoting elan:
> Hi Robert.
>
> >> b: a: [a 1 b 2]
> == [a 1 b 2]
> >> same? a b
> == true
>
> >> a: b: "xyz"
> == "xyz"
> >> d: c: reduce [a b]
> == ["xyz" "xyz"]
but you assigned d and c to the same block... why should they be different.
I think what you wanted to show was
b: a: "xyz"
c: reduce [a b]
same? first c second c
==true
here, even if we reduced a block which has words assigned to the same string, both still
reduce to the same string. Its like having a list with the same pointer twice.
proof:
insert first c "abc"
probe c
==["abcxyz" "abcxyz"]
since they are the same string, adding to first one will reflect in the second reference
too.
> >> same? d c
> == true
>
> Robert M. Münch wrote:
>
> >>>d: reduce [a b]
> >== [[a 1 b 2] [a 1 b 2]]
> >>>e: reduce [a b]
> >== [[a 1 b 2] [a 1 b 2]]
> >>>same? d e
> >== false
the above is not the same block
if you append to the block d, then it will not be reflected in block e.
continuing above code:
append d [test block]
probe d
==[[a 1 b 2] [a 1 b 2] test block]
probe e
==[[a 1 b 2] [a 1 b 2]]
this is why same? returns false, d and e aren't the same block.
> >So I tink for nested structures we would need something like
> a same?/deep.
> >Robert
when doing same?, same?/deep is actually implicit. you cannot have two same outer blocks
if their content is different. for example, I am always the same person. even if two
persons look at me, they still see the same person. where as if I had a twin, they looking
at each twin (altough looking exactly the same) is not the same person. Thus if two
persons are looking at the same person, they will implicitely look at the same eyes,
clothes, etc.
so:
((same?/deep block ) <> (same? block)) is impossible!
looking at the above example, I think equivalent? is really what you'd like, no?
this would return true if all items within two blocks contained the same data (but possibly
not the same data pointers).
same <> equivalent
if you want to know if two different blocks are equivalent, the = operator does dig deeply.
a: [a [1 [2]] [3 [4]]]
b: [a [1 [2]] [3 [4]]]
same? a b
==false
a = b
==true
a: [a [1 [2]] [3 [4]]]
b: [a [1 [2]] [3 [5]]]
same? a b
==false
a = b
==false
in conclusion, it might be that what you want is really a same?/content which would return
true if all elements of a block are the same, but does not check if the block itself
is the same. In that case, using /deep refinement for same might be misleading, because
other functions which use /deep, usually consider the outer-most block as part of what
it has to process.
Just adding my (possibly redundant) comments to the topic ;-)
cheers!
-MAx