Environment Diagrams

The questions below are due on Sunday July 06, 2025; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Link to relevant section of reading

Problem 0 - Translate Diagram to Code

In the white code box to the right of the 1 below, enter any valid Python program that, when executed, would lead to the diagram above.

Problem 1 - Function Definition

Create a diagram representing the result of executing the following code, consisting of a single function definition. Remember that a function object must contain the argument names, the function's body, and a pointer to the frame in which it was defined (its "enclosing frame").

def foo(n, k):
    n = n / (k + d)
    return n

ℹ️
🗑️

Problem 2 - Calling Functions

When calling a function, we follow several steps:

  1. Evaluate the function to be called, and its arguments (in order)
  2. Create a new frame for the function call, with the function's enclosing frame as its parent
  3. Bind the parameters of the function to the given arguments in this new frame
  4. Execute the body of the function in this new frame.

This process is demonstrated through the examples from the readings.

Note that in this interface, we specify each frame's parent with a line from the dot at the top of each frame to its parent. Frames can also have a return value. We will diagram the return value of the function using the return pointer at the bottom of the frame. If the return pointer isn't set, this corresponds to a function returning None.

For this problem, diagram the result of executing the following code. Include each of the frames generated by function calls. For this problem, there should be three total frames: the global frame (already provided), one generated by a call to foo and one generated by a call to bar.

def foo(n, k):
    n = n / (k + d)
    return n

d = 10
n = 2
z = foo(36, n)

ℹ️
🗑️

Next Exercise: Hip to Be Square
Back to exercises