Scheme Declaration and Assignment
Here is an example of a simple statement that effects the declaration of a variable and then an assignment of a value to it, a binding which will be visible in a Scheme program outside of that block's scope.
(define x null)This statement will define the identifier x and create space in memory for it. If you then, for example, type x, you get the value that you bound to it. For example,
x => emptyHowever, if you try to redefine x, Scheme won't allow it.
(define x 3) define: cannot redefine name: xSemantic structure of this statement:
Env_0 (define Var Env_1 = Env_0 + (Var) ;Var identifier is added to the environment expr Val_0 ;the evaluated equivalent of expr, Val_0, is added to the environment Env_2 = Env_1 + (Val_0) ;Val_0 is added Return: Val_0 ;Val_0 is returned to the toplevel scope ) Env_3 = Env_2 + (Var:=Val_0) ;the binding between the identifer Var and Val_0 appears in the toplevel environmentNow, in order assign another value to x, which doesn't have a defined type, we use the keyword set!. An example of this is shown here.
(set! x "hello world")A semantic interpretation:
Env_3 ;continuing from above ( set! Var ;should already exist in this scope expr Val_1 Env_4 = Env_3 + (Var:=Val_1) Env_4Simply typing x into an interactive Scheme system yields the following result:
x => "hello world"I do not believe that there is any I/O operation inherent in either of these statements, so I won't be discussing them here.