Variable Definitions

#drnsecsynsemvardefns#8548> Programs consist not only of function definitions but also variable definitions, but these weren't included in our first grammar. Here is the grammar rule for variable definitions:

#tabular8550#

The shape of a variable definition is similar to that of a function definition. It starts with a ``('', followed by the keyword <#61882#><#8559#>define<#8559#><#61882#>, followed by a variable, followed by an expression, and closed by a right parenthesis ``)'' that matches the very first one. The keyword <#61883#><#8560#>define<#8560#><#61883#> distinguishes variable definitions from expressions, but not from function definitions. For that, a reader must look at the second component of the definition. Next we must understand what a variable definition means. A variable definition like

<#8565#>(define<#8565#> <#8566#>RADIUS<#8566#> <#8567#>5)<#8567#>
has a plain meaning. It says that wherever we encounter <#61884#><#8571#>RADIUS<#8571#><#61884#> during an evaluation, we may replace it with <#61885#><#8572#>5<#8572#><#61885#>. When DrScheme encounters a definition with a proper expression on the right-hand side, it must evaluate that expression immediately. For example, the right-hand side of the definition
<#8577#>(define<#8577#> <#8578#>DIAMETER<#8578#> <#8579#>(*<#8579#> <#8580#>2<#8580#> <#8581#>RADIUS))<#8581#>
is the expression <#61886#><#8585#>(*<#8585#>\ <#8586#>2<#8586#>\ <#8587#>RADIUS)<#8587#><#61886#>. Its value is <#61887#><#8588#>10<#8588#><#61887#> because <#61888#><#8589#>RADIUS<#8589#><#61888#> stands for <#61889#><#8590#>5<#8590#><#61889#>. Hence, we can act as if we had written
<#8595#>(define<#8595#> <#8596#>DIAMETER<#8596#> <#8597#>10)<#8597#>
In short, when DrScheme encounters a variable definition, it determines the value of the right-hand side. For that step, it uses all those definitions that <#8601#>precede<#8601#> the current definition but not those that follow. Once DrScheme has a value for the right-hand side, it remembers that the name on the left-hand side stands for this value. Whenever we evaluate an expression, every occurrence of the <#61890#><#8602#>define<#8602#><#61890#>d variable is replaced by its value.
<#8607#>(define<#8607#> <#8608#>RADIUS<#8608#> <#8609#>10)<#8609#>
<#8610#>(define<#8610#> <#8611#>DIAMETER<#8611#> <#8612#>(*<#8612#> <#8613#>2<#8613#> <#8614#>RADIUS))<#8614#> 
<#70908#>;; <#61891#><#8615#>area<#8615#> <#8616#>:<#8616#> <#8617#>number<#8617#> <#8618#><#8618#><#8619#>-;SPMgt;<#8619#><#8620#><#8620#> <#8621#>number<#8621#><#61891#><#70908#> 
<#70909#>;; to compute the area of a disk with radius <#61892#><#8622#>r<#8622#><#61892#><#70909#> 
<#8623#>(d<#8623#><#8624#>efine<#8624#> <#8625#>(area<#8625#> <#8626#>r)<#8626#> 
  <#8627#>(*<#8627#> <#8628#>3.14<#8628#> <#8629#>(*<#8629#> <#8630#>r<#8630#> <#8631#>r)))<#8631#> 
<#8632#>(define<#8632#> <#8633#>AREA-OF-RADIUS<#8633#> <#8634#>(area<#8634#> <#8635#>RADIUS))<#8635#> 
<#8639#>Figure: An example of variable definitions<#8639#>
Consider the sequence of definitions in figure~#figdefinedvariables#8641>. As DrScheme steps through this sequence of definitions, it first determines that <#61893#><#8642#>RADIUS<#8642#><#61893#> stands for <#61894#><#8643#>10<#8643#><#61894#>, <#61895#><#8644#>DIAMETER<#8644#><#61895#> for <#61896#><#8645#>20<#8645#><#61896#>, and area is the name of a function. Finally, it evaluates <#61897#><#8646#>(area<#8646#>\ <#8647#>RADIUS)<#8647#><#61897#> to <#61898#><#8648#>314.0<#8648#><#61898#> and associates <#61899#><#8649#>AREA-OF-RADIUS<#8649#><#61899#> with that value.
<#8652#>Exercise 8.6.1<#8652#> Make up five examples of variable definitions. Use constants and expressions on the right-hand side. external Solution<#61900#><#61900#> <#8659#>Exercise 8.6.2<#8659#> Evaluate the following sequence of definitions
<#8665#>(define<#8665#> <#8666#>RADIUS<#8666#> <#8667#>10)<#8667#>
<#8668#>(define<#8668#> <#8669#>DIAMETER<#8669#> <#8670#>(*<#8670#> <#8671#>2<#8671#> <#8672#>RADIUS))<#8672#> 
<#8673#>(define<#8673#> <#8674#>CIRCUMFERENCE<#8674#> <#8675#>(*<#8675#> <#8676#>3.14<#8676#> <#8677#>DIAMETER))<#8677#> 
by hand.~ external Solution<#61901#><#61901#> <#8686#>Exercise 8.6.3<#8686#> Evaluate the following sequence of definitions
<#8692#>(define<#8692#> <#8693#>PRICE<#8693#> <#8694#>5)<#8694#>
<#8695#>(define<#8695#> <#8696#>SALES-TAX<#8696#> <#8697#>(*<#8697#> <#8698#>.08<#8698#> <#8699#>PRICE))<#8699#> 
<#8700#>(define<#8700#> <#8701#>TOTAL<#8701#> <#8702#>(+<#8702#> <#8703#>PRICE<#8703#> <#8704#>SALES-TAX))<#8704#> 
by hand.~ external Solution<#61902#><#61902#>