This preview shows pages 1–3. Sign up to view the full content.
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 (definestruct posn (x y)) (define (fmakeposn x0 y0) (local ((define x y0) (define y y0) (define (servicemanager msg) (cond [(symbol=? msg 'x) x] [(symbol=? msg 'y) y] [else (error 'posn "...")]))) servicemanager)) (define (fposnx p) (p 'x)) (define (fposny p) (p 'y)) Figure 116: A functional analog of posn Take a look at figure 116 . The lefthand side is the oneline definition of a posn structure. The righthand 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/curriculumZH50.html (1 of 20) [2/5/2008 4:58:51 PM] How to Design Programs: An Introduction to Computing and Programming To understand why fmakeposn is a constructor and why fposnx and fposny 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 fmakeposn encapsulates two variable definitions and one function definition. The two variables stand for the arguments of fmakeposn 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 aposn (fmakeposn 3 4)) (+ (aposn 'x) (aposn 'y)) to define and to compute with fmakeposn . Since selecting values is such a frequent operation, figure 116 introduces the functions fposnx and fposny , 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: (posnx (makeposn V1 V2)) = V1 and (posny (makeposn V1 V2)) = V2 where V1 and V2 are arbitrary values....
View
Full
Document
This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.
 Fall '07
 Fisler

Click to edit the document details