Assignments and Functions

An assignment can also occur in a function body:
<#44416#>(define<#44416#> <#44417#>x<#44417#> <#44418#>3)<#44418#>
<#44419#>(define<#44419#> <#44420#>y<#44420#> <#44421#>5)<#44421#> 
<#44422#>(d<#44422#><#44423#>efine<#44423#> <#44424#>(swap-x-y<#44424#> <#44425#>x0<#44425#> <#44426#>y0)<#44426#> 
  <#44427#>(b<#44427#><#44428#>egin<#44428#> 
    <#44429#>(set!<#44429#> <#44430#>x<#44430#> <#44431#>y0)<#44431#> 
    <#44432#>(set!<#44432#> <#44433#>y<#44433#> <#44434#>x0)))<#44434#> 
<#44435#>(swap-x-y<#44435#> <#44436#>x<#44436#> <#44437#>y)<#44437#> 
Here the function <#68098#><#44441#>swap-x-y<#44441#><#68098#> consumes two values and performs two <#68099#><#44442#>set!<#44442#><#68099#>s. Let us see how the evaluation works. Because <#68100#><#44443#>(swap-x-y<#44443#>\ <#44444#>x<#44444#>\ <#44445#>y)<#44445#><#68100#> is a function application, we need to evaluate the arguments, which are plain variables here. So we replace the variables with their (current) values:
<#44450#>(define<#44450#> <#44451#>x<#44451#> <#44452#>3)<#44452#>
<#44453#>(define<#44453#> <#44454#>y<#44454#> <#44455#>5)<#44455#> 
<#44456#>(d<#44456#><#44457#>efine<#44457#> <#44458#>(swap-x-y<#44458#> <#44459#>x0<#44459#> <#44460#>y0)<#44460#> 
  <#44461#>(b<#44461#><#44462#>egin<#44462#> 
    <#44463#>(set!<#44463#> <#44464#>x<#44464#> <#44465#>y0)<#44465#> 
    <#44466#>(set!<#44466#> <#44467#>y<#44467#> <#44468#>x0)))<#44468#> 
<#44469#>(swap-x-y<#44469#> <#44470#>3<#44470#> <#44471#>5)<#44471#> 
From here we proceed with the usual substitution rule for application:
<#44479#>(define<#44479#> <#44480#>x<#44480#> <#44481#>3)<#44481#>
<#44482#>(define<#44482#> <#44483#>y<#44483#> <#44484#>5)<#44484#> 
<#44485#>(d<#44485#><#44486#>efine<#44486#> <#44487#>(swap-x-y<#44487#> <#44488#>x0<#44488#> <#44489#>y0)<#44489#> 
  <#44490#>(b<#44490#><#44491#>egin<#44491#> 
    <#44492#>(set!<#44492#> <#44493#>x<#44493#> <#44494#>y0)<#44494#> 
    <#44495#>(set!<#44495#> <#44496#>y<#44496#> <#44497#>x0)))<#44497#> 
<#44498#>(b<#44498#><#44499#>egin<#44499#> 
  <#44500#>(set!<#44500#> <#44501#>x<#44501#> <#44502#>5)<#44502#> 
  <#44503#>(set!<#44503#> <#44504#>y<#44504#> <#44505#>3))<#44505#> 
That is, the application is now replaced by an assignment of <#68101#><#44509#>x<#44509#><#68101#> to the current value of <#68102#><#44510#>y<#44510#><#68102#> and of <#68103#><#44511#>y<#44511#><#68103#> to the current value of <#68104#><#44512#>x<#44512#><#68104#>. The next two steps are also the last ones and they thus accomplish what the name of the function suggests:
<#44517#>(define<#44517#> <#44518#>x<#44518#> <#44519#>5)<#44519#>
<#44520#>(define<#44520#> <#44521#>y<#44521#> <#44522#>3)<#44522#> 
<#44523#>(d<#44523#><#44524#>efine<#44524#> <#44525#>(swap-x-y<#44525#> <#44526#>x0<#44526#> <#44527#>y0)<#44527#> 
  <#44528#>(b<#44528#><#44529#>egin<#44529#> 
    <#44530#>(set!<#44530#> <#44531#>x<#44531#> <#44532#>y0)<#44532#> 
    <#44533#>(set!<#44533#> <#44534#>y<#44534#> <#44535#>x0)))<#44535#> 
<#44536#>(<#44536#><#44537#>void<#44537#><#44538#>)<#44538#> 
The value of the application is invisible because the last expression evaluated was a <#68105#><#44542#>set!<#44542#>-expression<#68105#>. In summary, functions with <#68106#><#44543#>set!<#44543#><#68106#> have results and effects. The result may be invisible.
<#44546#>Exercise 35.3.1<#44546#> Consider the following function definition:
<#44552#>(d<#44552#><#44553#>efine<#44553#> <#44554#>(f<#44554#> <#44555#>x<#44555#> <#44556#>y)<#44556#>
  <#44557#>(b<#44557#><#44558#>egin<#44558#> 
    <#44559#>(set!<#44559#> <#44560#>x<#44560#> <#44561#>y)<#44561#> 
    <#44562#>y))<#44562#> 
Is it syntactically legal or illegal?~ external Solution<#68107#><#68107#> <#44571#>Exercise 35.3.2<#44571#> Evaluate the following program by hand:
<#44577#>(define<#44577#> <#44578#>x<#44578#> <#44579#>3)<#44579#>
<#44580#>(d<#44580#><#44581#>efine<#44581#> <#44582#>(increase-x)<#44582#> 
  <#44583#>(b<#44583#><#44584#>egin<#44584#> 
    <#44585#>(set!<#44585#> <#44586#>x<#44586#> <#44587#>(+<#44587#> <#44588#>x<#44588#> <#44589#>1))<#44589#> 
    <#44590#>x))<#44590#> 
<#44591#>(increase-x)<#44591#> 
<#44592#>(increase-x)<#44592#> 
<#44593#>(increase-x)<#44593#> 
What is the result? What is <#68108#><#44597#>increase-x<#44597#><#68108#>'s effect?~ external Solution<#68109#><#68109#> <#44603#>Exercise 35.3.3<#44603#> Evaluate the following program by hand:
<#44609#>(define<#44609#> <#44610#>x<#44610#> <#44611#>0)<#44611#>
<#44612#>(d<#44612#><#44613#>efine<#44613#> <#44614#>(switch-x)<#44614#> 
  <#44615#>(b<#44615#><#44616#>egin<#44616#> 
    <#44617#>(set!<#44617#> <#44618#>x<#44618#> <#44619#>(-<#44619#> <#44620#>x<#44620#> <#44621#>1))<#44621#> 
    <#44622#>x))<#44622#> 
<#44623#>(switch-x)<#44623#> 
<#44624#>(switch-x)<#44624#> 
<#44625#>(switch-x)<#44625#> 
What is the result? What is <#68110#><#44629#>switch-x<#44629#><#68110#>'s effect?~ external Solution<#68111#><#68111#> <#44635#>Exercise 35.3.4<#44635#> Evaluate the following program by hand:
<#44641#>(define<#44641#> <#44642#>x<#44642#> <#44643#>0)<#44643#>
<#44644#>(define<#44644#> <#44645#>y<#44645#> <#44646#>1)<#44646#> 
<#44647#>(d<#44647#><#44648#>efine<#44648#> <#44649#>(change-to-3<#44649#> <#44650#>z)<#44650#> 
  <#44651#>(b<#44651#><#44652#>egin<#44652#> 
    <#44653#>(set!<#44653#> <#44654#>y<#44654#> <#44655#>3)<#44655#> 
    <#44656#>z))<#44656#> 
<#44657#>(change-to-3<#44657#> <#44658#>x)<#44658#> 
What is the effect of <#68112#><#44662#>change-to-3<#44662#><#68112#>? What is its result?~ external Solution<#68113#><#68113#>