Environment Diagrams
eharetea

Example environment diagrams in Scheme.

Example environment diagrams in Scheme.

Date Created:Saturday October 25th, 2008 05:26 PM
Date Modified:Tuesday January 19th, 2010 10:40 PM

1-global.jpg - 3Daet.com
First, define the "class":
(define (make-account balance)
  (define (withdraw amount)
   (if (>= balance amount)
       (begin (set! balance (- balance amount)) balance)
       "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
 (define (dispatch m)
    (cond ((eq? m 'withdraw) withdraw)
          ((eq? m 'deposit) deposit)
          (else (error "what the?" m)))) dispatch)
2-define.jpg - 3Daet.com
Now we create an instance:
(define acc (make-account 50))
3-instance.jpg - 3Daet.com
Now deposit some money:
((acc 'deposit) 40))
4-deposit.jpg - 3Daet.com
Now make a withdraw:
((acc 'withdraw) 60)
5-withdraw.jpg - 3Daet.com
Let's look at another example: Start out with Global:
1.jpg - 3Daet.com
(define x 4)
This sets a binding from x to the symbol 4.
2-define-x.jpg - 3Daet.com
Next, define baz:
(define (baz x)
  (define (* a b) (+ a b))
  (lambda (y) (* x y)))
This generates function bubbles that extends the global and binds the name baz to these bubbles. The left bubble points to the underlying code, the parameters and the body. The parameters in this case are x, and the body is the define and the lambda within the body of baz.
3-define-baz.jpg - 3Daet.com
Next, define foo:
(define foo (baz (* 3 10)))
Note that (* 3 10) is evaluated in the global, hence, it is tantamount to binding foo to (baz 30). This call, however, is more complex. First we need to initialize a new frame and bind some procedures, since baz is called: If you notice, first a new frame was created for the call to baz. The frame was initialized: 1) The frame was extended to the global since the right bubble of baz points to the global. 2) A binding for foo was set in the global and points to the lambda, the compound procedure returned by baz. This generates a pair of function bubbles with a parameter of y, and a body of (* x y). This function is an extension of the new frame. 3) A binding for x was set to 30, since the parameter for baz is x. 4) A binding for * was created within the new frame, along with a new function bubble pair for *, which extends this new frame as well.
4-define-foo.jpg - 3Daet.com
Note that we are defining foo in the global, and defining * in the frame that is generated upon a call to baz. Next, call foo. This generates two new frames since there is two procedure calls:
5-call-foo.jpg - 3Daet.com
First, (foo (* 2 x)) was evaluated with E1 as the current frame. This generated a new frame that extends E!, and binds y to 8 since (* 2 x) is (* 2 4) since x is defined in the global. Foo is basically evaluated as (foo 8). This is creates another frame, the call is to the * defined in baz. Since the parameters were a and b, we bind the values of x to a, where x is 30 because that was defined when foo was defined as (baz 30), and then y to b where y is 8 since we just called (foo 8). Then the body of * is evaluated, (+ 30 8), and 38 is returned.