Scheme Procedure Definition Using Define Statement
Function definition using define. Function will be accessible throughout the execution. Example:
(define square
(lambda (x) (* x x)))
Semantic structure:
Env_0
( define FunctionID
Env_1 = Env_0 + (FunctionID) ;FunctionID is added to the environment
( lambda (Paramlist)
Env_10 = Env_1 + (Paramlist) ;the Paramlist is added to the environment in side the function definition, but is not
;visible outside, so I call the resulting environment Env_10
expr
Val_0 ;Val_0 results from the evaluation of expr with the given Params
Env_11 = Env_10 + (FunctionID:Val_0) ;FunctionID is now bound to the newly created Val_0
Env_2 = Env_11 + (FunctionID:=Paramlist expr, Env_0) ;As far as the outside environment is concerned, the function exists as one
;undissectable procedure, added the environment as a whole. The environment
;in which the function was defined is also part of its definition, because
;of the lexical nature of scoping in Scheme.
The call to function square, with an argument 5.
(square 5)Semantic structure:
Env_2
( FunctionID
Arglist
Env_20 = Env_2 + (Paramlist:=Arglist) ;The values in the Arglist are passed to the corresponding
;parameters in the function definition
expr ;expr is acquired from the function definition and evaluated, given the Args
Val_1 ;yielding Val_1
Return Val_1 ;which is returned
Env_3 = Env_2 + (FunctionID Arglist:=Val_1) ;The new environemt contains what was held in the environment
;before the function
;call, in addition to a new binding between the (FunctionID Arglist) and the value
;Val_1
I do not plan to discuss I/O changes in this example, as I believe no significant side effects occur with regards to input-output operations.
Recursion?