title: Lexical vs dynamic scoping in terms of SICP's Environment Model of Evaluation
body: <p>In <a href="http://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-21.html#%_sec_3.2.2" rel="nofollow noreferrer">Section 3.2.2</a> of SICP the execution of the following piece of code </p>
  
  <pre><code>(define (square x)
    (* x x))
  (define (sum-of-squares x y)
    (+ (square x) (square y)))
  (define (f a)
    (sum-of-squares (+ a 1) (* a 2)))
  
  (f 5)
  </code></pre>
  
  <p>is explained in terms of <a href="http://mitpress.mit.edu/sites/default/files/sicp/full-text/book/ch3-Z-G-6.gif" rel="nofollow noreferrer">this diagram</a>.</p>
  
  <p>Each time a function is applied, a new frame is created (labeled by <code>E1</code> through <code>E4</code>)  which represents a set of bindings between symbols and values. When a symbol is not bound in a frame, that frame's enclosing environment is queried for a binding of that particular symbol.</p>
  
  <p>The interesting thing about this diagram is that all the frames labelled by <code>E</code> is contained in the global environment. The text explains that this is because the functions was defined in the global environment, but does not elaborate on the issue:</p>
  
  <blockquote>
    <p>Notice that each frame created by <code>square</code> points to the global environment, since this is the environment indicated by the <code>square</code> procedure object.</p>
  </blockquote>
  
  <p>If instead frames where contained in the environment that the function was called in, say <code>E3</code> was contained in <code>E2</code> which in turn was contained in <code>E1</code>, would that be a valid model of how a dynamically scoped language works? Also, is the way that the frames in the diagram have the same 'parent' environment because Scheme is lexically scoped?</p>