-
It's my understanding that:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 15 replies
-
For example: Now, if I'm done with the latter binding, how do I get rid of it so that I can use the former? |
Beta Was this translation helpful? Give feedback.
-
Bindings in the REPL can be thought of as being part of a new let whose scope extends to the end of the interactive session. For your example, you have let val OneMile = 1.609 in
let val OneMile = 1609 in
let val it = OneMile * 55 in
.... There is no way to revoke an existing binding. What you want is something that behaves like |
Beta Was this translation helpful? Give feedback.
-
(1) In SML, values in general are immutable, except for certain values that are mutable, for instance "ref" values for which there is an assignment operator.
x is "bound to" (i.e., names) the immutable integer value 3.
x is bound to a reference (or "ref cell") whose initial value is the integer 3. The type of x is "int ref". The value stored in the ref cell can be changed by the assignment operator ":=", as in
The value of x is not changed -- it is still bound to the same ref cell, but the contents of that ref cell has been modified and is now 4. (2,3) Yes, each variable binding has what is called a "static scope", which is the part of the program (expression, declaration) where that binding "applies" or is "visible".
will print the two integers 2 and 3. The "inner" binding of x (to 2) applies to the first call of print while the "outer" binding of x (to 3) applies to the second call of print. The second declaration of x "shadows" the first declaration of x in its scope, which is the (first) expression "print (Int.toString x)". When you ask for the value of x, you will get the value bound to x by the innermost declaration. |
Beta Was this translation helpful? Give feedback.
-
A comment on the terminology. Carrying over ideas from other languages, it is common for SML beginners to think of a declaration like
as an "assignment" of a value to a "variable" x, where it is implicitly assumed that "x" designates a location in memory, and the declaration is storing the value 3 at that location.
the variable "x" stands for some arbitrary, unspecified number. This is similar to the use of variables when specifying a function
Here x represents an arbitrary argument value to which the function could be applied. When the function is applied to a particular value, as in
then x is "bound" to 2 (or becomes a name for 2) and the "body" of the function, x+1 is evaluated to return the value 3. This function application can be (approximately) translated to the expression
In summary, using the term "assignment" when talking about a variable declaration in a language like ML has the wrong connotation. |
Beta Was this translation helpful? Give feedback.
Bindings in the REPL can be thought of as being part of a new let whose scope extends to the end of the interactive session. For your example, you have
There is no way to revoke an existing binding. What you want is something that behaves like
local
(but the REPL does not provide that).