Quote:Original post by Alpha_ProgDes
*** Source Snippet Removed ***
i take it "we" is basically a new object every time i redefine it. something like:
*** Source Snippet Removed ***
i'm using Dr. Scheme by the way.
I'm not 100% sure what you really mean. There are multiple ways to answer that question.
'define' (re)binds the identifier to a value. If you write this in Scheme:
(define x 5)(define (f) x)(f) -> 5(define x 6)(f)--> 6
However, you cannot use this to achieve runtime mutability: if you use 'define' in a new environment/scope, the old value will be restored:
(define x 5)(define (f) (define x 6) 'done)x--> 5(f)--> donex--> 5
The 'define' in 'f' creates a new environment (scope limited to f's body), in which a new 'x' is created.
To show this is not a simple matter, consider the following O'Caml code:
let x = 5;;let f () = x;;f ();;--> 5let x = 6;;f ();;--> 5
Don't break your head over it for now, chapter 4 will make this more clear where you'll write your very own Scheme interpreter. I would suggest you don't think of 'define' as a way to redefine values; act as if it's only allowed to use one define per identifier per scope.
edit: I see that Scheme (DrScheme anyway) prevents you from 'defining' the same identifier twice in an environment which is not the top-level one, which is good.
There's also the "referential transparency" issue. In Scheme, everything works through pointers/references. If you were to work in C++ this way, you could have two different pointers to 5, without them being equal because they point to different memory cells:
int a = 5, b = 5;int* p = &aint* q = &bp != q
As far as I know, there is no way to make this kind of distinction in Scheme with integers. Some languages take this approach everywhere: if you have two data structures containing the same data, they are indiscernible from each other. Only "object value" counts in these languages. Other languages such as C/C++/java/C#/... take into account "object identity": objects can represent the same values, yet have different identities (hence you need to use .equals() instead of == in java, etc.)