This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 40 Mutable Structures Encapsulating and managing state variables is similar to forming and managing structures. When we first apply a function that abstracts over state variables we provide initial values for some of the variables. The service manager serves the (current) value of these variables, which is similar to extracting the values of fields in structures. Not surprisingly then, the technique can simulate the constructors and selectors of a define- struct definition. This simulation naturally suggests the introduction of functions that modify the value in a structure's field. The following subsections spell out the details behind this idea; the last subsection generalizes it to vectors. 40.1 Structures from Functions (define-struct posn (x y)) (define (f-make-posn x0 y0) (local ((define x y0) (define y y0) (define (service-manager msg) (cond [(symbol=? msg 'x) x] [(symbol=? msg 'y) y] [else (error 'posn "...")]))) service-manager)) (define (f-posn-x p) (p 'x)) (define (f-posn-y p) (p 'y)) Figure 116: A functional analog of posn Take a look at figure 116 . The left-hand side is the one-line definition of a posn structure. The right-hand side is a functional definition that provides almost all the same services. In particular, the definition provides a constructor that consumes two values and constructs a compound value, and two selectors for extracting the values that went into the construction of a compound value. file:///C|/Documents%20and%20Settings/Linda%20Graue...How%20to%20Design%20Programs/curriculum-Z-H-50.html (1 of 20) [2/5/2008 4:58:51 PM] How to Design Programs: An Introduction to Computing and Programming To understand why f-make-posn is a constructor and why f-posn-x and f-posn-y are selectors, we can discuss how they work, and we can confirm that they validate the expected equations. Here we do both, because the definitions are unusual. The definition of f-make-posn encapsulates two variable definitions and one function definition. The two variables stand for the arguments of f-make-posn and the function is a service manager; it produces the value of x when given 'x and the value of y when given 'y . In the preceding section, we might have written something like (define a-posn (f-make-posn 3 4)) (+ (a-posn 'x) (a-posn 'y)) to define and to compute with f-make-posn . Since selecting values is such a frequent operation, figure 116 introduces the functions f-posn-x and f-posn-y , which perform these computations. When we first introduced structures rigorously in intermezzo 1, we said that the selectors and constructors can be described with equations. For a definition such as that for posn , the two relevant equations are: (posn-x (make-posn V-1 V-2)) = V-1 and (posn-y (make-posn V-1 V-2)) = V-2 where V-1 and V-2 are arbitrary values....
View Full Document
- Fall '07
- Extensional definition, Mutators, Design Programs