04: OOP, Environments, and Local State

May 05, 2020

This article is a part of a series where I go through teachyourselfcs. If you would like to start at the beginning start here.



OOP is a implementation of lambda.

instantiation variable
Class variables

Anytime you change the value of something use a exclamation point to signify you are not doing functional programming.


OOP implements inheritance through delegation (assigning to something else).

Child looks for the methods and if it can’t find it the message is sent to the parent.

default-method: what to do when the parent can’t handle the message.

initialize: method only used once to setup the class

dispatch procedure: takes message and returns a method.


OOP: With every object you have the procedures that work on it.

Polymorphism: Ability to work with other types in the sending op messages.

Garbage collection: automatic storage management.


global variables: available to anyone
class variables: available to that class
instance variables available to that instance

Persistent local state variables. Lambda inside a let creates local state variables.

Can wrap lambda in parenthesis to invoke it after creating it.

Substitution model (eval exp):
Eval all sub expressions
apply procedure to arg list
substitute args for params in body
eval modified body

Environment model (eval exp in env):
Eval all sub expressions.
apply procedure to arg list
make an env with params bound to args
Eval body in new env.


maps variables to values
pointer to “enclosing environment”
All evals are done in a environment.

Creates a new environment when you invoke a procedure.

Can create a pointer to another environment to inherit those variables.

1.) look in current env, if found return value
2.) look in parent env, if found return value
3.) if not in parent env returne UNBOUND VARIABLE error.

Lexical scoping: extends lambda environment

dynamic scope: extends specific environments.


Professor out, more review on environments.



4A doesn’t have code problems.


(define (make-account init-amount)
(let ((balance init-amount) (transactions '()))
    (define (withdraw amount)
        (set! transactions (append transactions (list 'withdraw amount)))
        (set! balance (- balance amount)) balance)
    (define (deposit amount)
        (set! transactions (append transactions (list 'deposit amount)))
        (set! balance (+ balance amount)) balance)
    (define (dispatch msg)
            ((eq? msg 'withdraw) withdraw)
            ((eq? msg 'deposit) deposit)
            ((eq? msg 'balance) balance)
            ((eq? msg 'transactions) transactions)))


3.1 Assignment and local state

Delayed evaluation: decoupling time from events during evaluation.

Assignment operator: allows the ability to change the value associated with a variable.

set!: exclamation shows reassignment.

Reassignment makes the substitution model no longer an adequate model, because the substitution model does not keep track of occurences, it uses the first occurence to sustitute in the body.

Reassignment can make 2 identical function calls have different results. The term for not using reassigment in functions is “referential transparency”.

Not having referential transparency means that you are unable to know what an object is without observing its state.

Programming that makes extensive use of reassignment is considered “imperative programming”.

This opens up the program to bugs not able to be created in functional programming.

3.2 Evironment model of evaluation

Environment: a place in which variables are stored.

Environment is a sequence of frames, each frame a table of bindings.

Each frame points to a parent environment unless it is the global environment.

When defining a procedure the global env creates a binding tying the name of the procedure to the lambda procedure itself.

State within a procedure is bound within the frame of the procedure and not the environment of the enclosing procedure.

Software Engineer in Austin, Texas