lecture13 - E NVIRONMENTS AND L OCAL S TATE 13 GEORGE WANG

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: E NVIRONMENTS AND L OCAL S TATE 13 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley July 14, 2010 1 Above the Line: Review Things we talked about: define-class, instantiate, method, ask, instance-vars, instantiation variables, class-vars, parent, default-method, usual, initialize 2 The Line ??? define -class , method , instantiate , ask, instance -vars , class -vars , parent , default -method , usual Object Oriented Programming Message Passing Inheritance Local State This is essentially the line were talking about when we talk about above the line OOP. We want to talk about how it is that we implement message passing and inheritance and local state, and thats what well be attacking today and tomorrow. A local variable is one that is only available in a specific small portion of the program. In Scheme, well generally be talking about variables that are only available within a specific procedure. As an example of local variable, weve seen that let is able make them. A state variable is one that remembers its value from one invocation to the next, and thats not something weve seen before. 1 3 From Functional to Local State Before we can attack local state, lets try to remember some information in a global variable: ;;;;; In file cs61a/lectures/3.1/count1.scm (define counter 0) (define (count) (set! counter (+ counter 1)) counter) > (count) 1 > (count) 2 We can see that weve created a state variable. The variable counter is able to remember its value from one call of count to the next. Whats new here is the special form set! that allows us to change the value of a variable. This is not like let , which creates a temporary, local binding. This makes a permanent change in some variable that already existed. This is something we saw earlier this week with OOP, and the syntax is just like define (but not the abbreviation for defining a function): it takes an unevaluated name and an expression whose value provides the new value. Again, we cant just use the substitution model of evaluation. If you think about the substitution model where we substitute variable names with values, we end up with: (set! 0 (+ 0 1)) Instead, a better way to think about variables is that theyre like addresses written on postcards. If youInstead, a better way to think about variables is that theyre like addresses written on postcards....
View Full Document

This note was uploaded on 08/14/2010 for the course EECS 61A taught by Professor Harvey during the Summer '08 term at University of California, Berkeley.

Page1 / 5

lecture13 - E NVIRONMENTS AND L OCAL S TATE 13 GEORGE WANG

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online