Multiple Environments
Tracing a User-Defined Function
Code | Behind the Scenes |
---|---|
Def Statement | A new function is created! The name is bounded to the function in the current frame |
Call expression | Evaluate the operator and the operand The functions is called on the arguments |
Performing the function call | A new frame is created! The function parameters are bound to the arguments The body executes in the new environment |
Multiple Environments in One Diagram
- Remember, the earlier the frame is, the more towards the bottom it is in the environmental diagram.
- For nested functions, we first evaluate whatever the operands of the function is.
- We first draw a new frame for the operand function.
- We then pass the return value of the operand function to the operator function.
- Environment == Sequence of frames
- Only a global frame
- local frame followed by a global frame.
- we can find any given environment by following the parents of the frames.
- Names have no meaning without an environment.
- Every expression is evaluated in the context of an environment.
- This means that any name is evaluated to the value which is bound to that name in the earliest frame of the current environment containing the name.
- Every expression is evaluated in the context of an environment.
Names can have different meanings in different Environments
- This is because different environments have different combinations of frames.
- Within each frame, there is a possibility of a particular name being defined to another value.
- It is important to note that a call expression and the actual function body are evaluated in seperate environments.
- The call expression belongs to a parent frame (a “later” environment)
- The function body creates another frame (an “earlier” environment)
- Because of this, we have have functions that have the same name as their parameters.