ANNOUNCEMENT: There will be a question and answer review session on Friday December 5th at 9:30 in Ryerson 251 (The usual class time and place). More on Frames: - The frame-state-environment model has the following operations implemented for dealing with frames: - Create new (empty) frame pointing the a given environment - Remove a frame from the top of an environment - Add a variable to a frame, bind it to a symbol - Assign a value to a variable - The notes from last time regarding the frame usage of define and set! weren't quite right. If a variable is not bound, define will create a new binding IN THE LOCAL FRAME it is called in. If define is called on its own (i.e. not nested in anything) then the binding will occur in the global frame. If a variable has been previously bound, then define reassigns a new symbol to it in the current frame. If a variable is not bound, set! does not add a binding, and is an error. set! will only assign a value to an already existing variable. These properties are clarified with the set!/define examples in the DEMO. - The following define actions are handled my the model in the following ways: - (let ((var1 arg1) ... (varn argn)) body) - creates a new frame - binds var1...varn in that frame - assigns values of arg1...argn to variables bound to var1...varn - evaluates body with new environment - removes the frame it created This is handled in exactly the same way that (((lambda var1....varn) body) arg1....argn) is handled. - (begin p1 ... pn) - performs what p1 ... pn indicates (in that order) - evaluates to value of pn - (define z (cons x y)) - creates a bundle of 2 new variables - assigns value of x to one and value of y to other - symbols that are associated with x and y are (car z) and (cdr z), respectively - The place where variables are allocated is called the "heap". Garbage collection is the hidden service that allocates and deallocates memory in the heap. Equality in Scheme: - equal?, eqv?, and eq? are three functions used to test equality of objects in Scheme. eqv? acts as a test for pointer equality, checking whether two objects are located in the same point in memory. eq? is a simplify version of eqv? that runs a little faster in some instances. equal? instead tries to compare the values of two objects. See the DEMO of equality testing with looped zerolists. Note that equal? applied to 2 different looped zerolists enters an infinite loop, as it tries to check that the 2 infinite sequences are equal. make_counter Example: - make_counter starts a new variable count_value in a frame and produces an increment function that always acts on that particular instance of count_value in that particular frame. So, you get a function that increments an encapsulated count variable everytime it is called. This is the computational essence of object-oriented programming. Look for more on this next lecture.