<#8730#>(define-struct<#8730#> <#8731#>point<#8731#> <#8732#>(x<#8732#> <#8733#>y<#8733#> <#8734#>z))<#8734#> <#8735#>.<#8735#>Since <#61911#><#8739#>point<#8739#><#61911#>, <#61912#><#8740#>x<#8740#><#61912#>, <#61913#><#8741#>y<#8741#><#61913#>, and <#61914#><#8742#>z<#8742#><#61914#> are variables and the parentheses are placed properly, it is a proper definition of a structure. In contrast,
<#8747#>(define-struct<#8747#> <#8748#>(point<#8748#> <#8749#>x<#8749#> <#8750#>y<#8750#> <#8751#>z))<#8751#>and
<#8759#>(define-struct<#8759#> <#8760#>point<#8760#> <#8761#>x<#8761#> <#8762#>y<#8762#> <#8763#>z)<#8763#>are improper definitions because <#61915#><#8767#>define-struct<#8767#><#61915#> is not followed by a single variable name and a sequence of variables in parentheses. A <#61916#><#8768#>define-struct<#8768#><#61916#> definition introduces new primitive operations. The names of these operations are formed from those that occur in the definition. Suppose a data structure definition has the following shape:
<#8773#>(define-struct<#8773#> <#8774#>c<#8774#> <#8775#>(s-1<#8775#> <#8776#>...<#8776#> <#8777#>s-n))<#8777#>Then Scheme introduces the following primitive operations:
Let us return to the <#61928#><#8805#>points<#8805#><#61928#> structures. Since the list of fields
contains three names, <#61929#><#8806#>(make-point<#8806#>\ <#8807#>u<#8807#>\ <#8808#>v<#8808#>\ <#8809#>w)<#8809#><#61929#> is a value if
<#61930#><#8810#>u<#8810#><#61930#>, <#61931#><#8811#>v<#8811#><#61931#>, and <#61932#><#8812#>w<#8812#><#61932#> are values.
Now we are in a position to understand the evaluation rules of the new
primitives. If <#61933#><#8813#>c-s-1<#8813#><#61933#> is applied to a <#61934#><#8814#>c<#8814#><#61934#> structure, it
returns the first component of the value. Similarly, the second selector
extracts the second component, the third selector the third component, and
so on. The relationship between the new data constructor and the selectors
is best characterized with n equations:
<#62007#>Figure: <#9127#>Scheme Beginning Student<#9127#>: The full grammar<#62007#> <#8819#>(c-s-1<#8819#> <#8820#>(make-c<#8820#> <#8821#>V-1<#8821#> <#8822#>...<#8822#> <#8823#>V-n))<#8823#> <#8824#>=<#8824#> <#8825#>V-1<#8825#>
<#8826#>
where <#61935#><#8837#>V-1<#8837#>\ <#8838#>...<#8838#>\ <#8839#>V-n<#8839#><#61935#> is a sequence of values that is as long
as <#61936#><#8840#>s-1<#8840#>\ <#8841#>...<#8841#>\ <#8842#>s-n<#8842#><#61936#>.
For our running example, we get the equations
<#8847#>(point-x<#8847#> <#8848#>(make-point<#8848#> <#8849#>V<#8849#> <#8850#>U<#8850#> <#8851#>W))<#8851#> <#8852#>=<#8852#> <#8853#>V<#8853#>
<#8854#>(point-y<#8854#> <#8855#>(make-point<#8855#> <#8856#>V<#8856#> <#8857#>U<#8857#> <#8858#>W))<#8858#> <#8859#>=<#8859#> <#8860#>U<#8860#>
<#8861#>(point-z<#8861#> <#8862#>(make-point<#8862#> <#8863#>V<#8863#> <#8864#>U<#8864#> <#8865#>W))<#8865#> <#8866#>=<#8866#> <#8867#>W<#8867#>
In particular, <#61937#><#8871#>(point-y<#8871#>\ <#8872#>(make-point<#8872#>\ <#8873#>3<#8873#>\ <#8874#>4<#8874#>\ <#8875#>5))<#8875#><#61937#> is equal to
<#61938#><#8876#>4<#8876#><#61938#>, and <#61939#><#8877#>(point-x<#8877#>\ <#8878#>(make-point<#8878#>\ <#8879#>(make-point<#8879#>\ <#8880#>1<#8880#>\ <#8881#>2<#8881#>\ <#8882#>3)<#8882#>\ <#8883#>4<#8883#>\ <#8884#>5))<#8884#><#61939#>
evaluates to <#61940#><#8885#>(make-point<#8885#>\ <#8886#>1<#8886#>\ <#8887#>2<#8887#>\ <#8888#>3)<#8888#><#61940#> because the latter is also a value.
The predicate <#61941#><#8889#>c?<#8889#><#61941#> can be applied to any value. It returns
<#61942#><#8890#>true<#8890#><#61942#> if the value is of kind <#61943#><#8891#>c<#8891#><#61943#> and <#61944#><#8892#>false<#8892#><#61944#> otherwise.
We can translate both parts into equations. The first one,
<#8897#>(c?<#8897#> <#8898#>(make-c<#8898#> <#8899#>V-1<#8899#> <#8900#>...<#8900#> <#8901#>V-n))<#8901#> <#8902#>=<#8902#> <#8903#>true<#8903#> <#8904#>,<#8904#>
relates <#61945#><#8908#>c?<#8908#><#61945#> and values constructed with <#61946#><#8909#>make-c<#8909#><#61946#>; the second one,
<#8914#>(c?<#8914#> <#8915#>V)<#8915#> <#8916#>=<#8916#> <#8917#>false<#8917#><#70917#>; if <#61947#><#8918#>V<#8918#><#61947#> is a value not constructed with <#61948#><#8919#>make-c<#8919#><#61948#> , <#70917#>
relates <#61949#><#8923#>c?<#8923#><#61949#> to all other values.
Again, the equations are best understood in terms of our example. Here are
the general equations:
<#8928#>(point?<#8928#> <#8929#>(make-point<#8929#> <#8930#>V<#8930#> <#8931#>U<#8931#> <#8932#>W))<#8932#> <#8933#>=<#8933#> <#8934#>true<#8934#>
<#8935#>(point?<#8935#> <#8936#>U)<#8936#> <#8937#>=<#8937#> <#8938#>false<#8938#> <#70918#>; if <#61950#><#8939#>U<#8939#><#61950#> is value, but not a <#61951#><#8940#>point<#8940#><#61951#> structure.<#70918#>
Thus, <#61952#><#8944#>(point?<#8944#>\ <#8945#>(make-point<#8945#>\ <#8946#>3<#8946#>\ <#8947#>4<#8947#>\ <#8948#>5))<#8948#><#61952#> is <#61953#><#8949#>true<#8949#><#61953#> and
<#61954#><#8950#>(point?<#8950#>\ <#8951#>3)<#8951#><#61954#> is <#61955#><#8952#>false<#8952#><#61955#>.
<#8955#>Exercise 8.7.1<#8955#>
Explain why the sentences are legal <#61961#><#8985#>define-struct<#8985#><#61961#>
definitions or how they fail to be legal <#61962#><#8986#>define-struct<#8986#><#61962#>
definitions.~ Solution<#61963#><#61963#>
<#8992#>Exercise 8.7.2<#8992#>
<#9019#>Exercise 8.7.3<#9019#>
<#9027#>(define-struct<#9027#> <#9028#>ball<#9028#> <#9029#>(x<#9029#> <#9030#>y<#9030#> <#9031#>speed-x<#9031#> <#9032#>speed-y))<#9032#>
Determine the results of the following expressions:
Also check how DrScheme deals with the following expressions:
Verify your solutions with DrScheme.~ Solution<#61975#><#61975#>