Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: On mutability and sameness

From: joel:neely:fedex at: 16-Jun-2001 23:17

Hi, Ken, I think the thread has become tangled enough that way-losing has become proactively facilitated! (Can you tell that I'm a Dilbert fan? ;-) Ken Anthony wrote:
> Forgive me, perhaps I've just lost my way in the thread; > > > a: 12:30 ; == 12:30 > > a/hour: 13 ; == 13:30 > > a ; == 12:30 > > I get 13:30 for that last line. >
You should get 13:30. Note that the phrase just before the part you quoted said "once upon a time". An earlier version of REBOL exhibited the behavior quoted above, but that was long ago in a galaxy far, far away.
> I seem to be confused by what I thought to be a very simple > concept. Unless we've entered the twilight zone all computer > memory still only have two or three attributes; value, > address and perhaps bit-width which can be ignored for now. >
Bear in mind that we're talking about REBOL values, not computer memory. REBOL values have type, and what/how REBOL does with a value is influenced by its type.
> All variables are either direct or indirect addressed > regardless of the language, but I don't think that is even > germain to this issue. >
IMHO that *is* the issue.
> equal? is defined as "Returns TRUE if the values are equal." > > Which simply means regardless of where it is stored the bits > are the same (I would think.) >
EQUAL? tests whether *content* is the same, regardless of the data type; in the case of a reference type (your "indirect addressed" case) that may require following the reference to the actual content.
> same? is defined as "Returns TRUE if the values are > identical." >
Well, it's not the first time the on-line help favors brevity over clarity and completeness.
> Here's my confusion. I assume that equal? and same? are not > aliases for the same function. >
You are correct. They are not.
> So my understanding would hinge on the meaning of identical. > The only meaningful distinction I can come up with is that > identical means at the same address. >
Only if there's an "address" involved... see below.
> if that's the case (and how could it possibly be otherwise?) > then... > > a: 1:30 > b: 1:30 > same? a b ;==True > > ...confuses the hell out of me! Does anyone have an > explanation? >
I hate to descend to a low-level language, but consider the behavior of the following c program: 8<------------------------------------------------------------ #include <stdio.h> int main (int nargs, char *args[]) { char *a = "Hello!"; char *b = a; char c[] = {'H','e','l','l','o','!','\0'}; printf ("a same b: %s\n", (a == b) ? "yes" : "no" ); printf ("a same c: %s\n", (a == c) ? "yes" : "no" ); printf ("a equal b: %s\n", (strcmp (a, b) == 0) ? "yes" : "no" ); printf ("a equal c: %s\n", (strcmp (a, c) == 0) ? "yes" : "no" ); exit (0); } 8<------------------------------------------------------------ When run, it produces the following output: 8<------------------------------------------------------------ $ ./cmp a same b: yes a same c: no a equal b: yes a equal c: yes $ 8<------------------------------------------------------------ For simple types (such as int) == tests whether the values are the same. For pointer types == tests whether the pointers are the same. But this means that if you want to test for equality of the data POINTED TO by the pointers, you have to use a different function (strcmp). That's awkward. REBOL assumes that we are more interested in data than in mechanism, so EQUAL? always tests equality of data. For reference types (e.g., any series) EQUAL? looks *through* the reference to the data and checks for equality, while for non-reference types (e.g., integers or times) EQUAL? looks directly at the data and checks for equality. For reference types, SAME? looks *at* the reference itself to see if the references are the same. For non-reference types (e.g., integers or times) there are no references, so SAME? looks at the only thing it can -- the data values. Hope this helps! -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com