Mutable Vectors

Recall from intermezzo~#secintcost#53548> that vectors, like structures, are compound values. To extract a value from a structure, programs use selector operations. To extract a value from a vector, programs use natural numbers as indices. Hence, functions that process vectors defer to auxiliary functions that process vectors and natural numbers. Not surprisingly, vectors, like structures, are mutable compound values. The only mutator for vectors is <#69384#><#53549#>vector-set!<#53549#><#69384#>, a function that consumes a vector, an index, and a value. Thus, for example, the following program evaluates to <#69385#><#53550#>'<#53550#><#53551#>blank<#53551#><#69385#>:
<#53556#>(define<#53556#> <#53557#>X<#53557#> <#53558#>(vector<#53558#> <#53559#>'<#53559#><#53560#>a<#53560#> <#53561#>'<#53561#><#53562#>b<#53562#> <#53563#>'<#53563#><#53564#>c<#53564#> <#53565#>'<#53565#><#53566#>d))<#53566#>
<#53567#>(b<#53567#><#53568#>egin<#53568#> 
  <#53569#>(vector-set!<#53569#> <#53570#>X<#53570#> <#53571#>0<#53571#> <#53572#>'<#53572#><#53573#>blank)<#53573#> 
  <#53574#>(vector-set!<#53574#> <#53575#>X<#53575#> <#53576#>1<#53576#> <#53577#>'<#53577#><#53578#>blank)<#53578#> 
  <#53579#>(vector-set!<#53579#> <#53580#>X<#53580#> <#53581#>2<#53581#> <#53582#>'<#53582#><#53583#>blank)<#53583#> 
  <#53584#>(vector-set!<#53584#> <#53585#>X<#53585#> <#53586#>3<#53586#> <#53587#>'<#53587#><#53588#>blank)<#53588#> 
  <#53589#>(vector-ref<#53589#> <#53590#>X<#53590#> <#53591#>2))<#53591#> 
The four <#69386#><#53595#>vector-set!<#53595#><#69386#> expressions change <#69387#><#53596#>X<#53596#><#69387#> so that all of its four fields contain <#69388#><#53597#>'<#53597#><#53598#>blank<#53598#><#69388#>. The last expression extracts the value of one of the fields. In general, an evaluation concerning mutable vectors proceeds just like an evaluation for mutable structures. In particular, a <#69389#><#53599#>vector<#53599#><#69389#> expression introduces a new definition:
  <#53604#>(list<#53604#> <#53605#>(vector<#53605#> <#53606#>1<#53606#> <#53607#>2<#53607#> <#53608#>3))<#53608#>
<#53609#>=<#53609#> <#53610#>(list<#53610#> <#53611#>v)<#53611#> 
  <#53612#>;; add to top-level definitions: <#53612#> 
  <#53613#>(define<#53613#> <#53614#>v<#53614#> <#53615#>(vector<#53615#> <#53616#>1<#53616#> <#53617#>2<#53617#> <#53618#>3))<#53618#> 
The variable name <#69390#><#53622#>v<#53622#><#69390#> is new and unique. Similarly, a <#69391#><#53623#>vector-set!<#53623#><#69391#> expression modifies a part of a vector definition:
  <#53628#>(set-vector!<#53628#> <#53629#>(vector<#53629#> <#53630#>1<#53630#> <#53631#>2<#53631#> <#53632#>3)<#53632#> <#53633#>0<#53633#> <#53634#>'<#53634#><#53635#>a)<#53635#>
<#53636#>=<#53636#> <#53637#>(define<#53637#> <#53638#>v<#53638#> <#53639#>(vector<#53639#> <#53640#>1<#53640#> <#53641#>2<#53641#> <#53642#>3))<#53642#> 
  <#53643#>(set-vector!<#53643#> <#53644#>v<#53644#> <#53645#>0<#53645#> <#53646#>'<#53646#><#53647#>a)<#53647#> 
<#53648#>=<#53648#> <#53649#>(define<#53649#> <#53650#>v<#53650#> <#53651#>(vector<#53651#> <#53652#>'<#53652#><#53653#>a<#53653#> <#53654#>2<#53654#> <#53655#>3))<#53655#> 
  <#53656#>(<#53656#><#53657#>void<#53657#><#53658#>)<#53658#> 
Finally, effects to vectors are shared just like effects to structures.
<#53664#>Exercise 40.4.1<#53664#> Evaluate the following program:
<#53670#>(define<#53670#> <#53671#>X<#53671#> <#53672#>(vector<#53672#> <#53673#>0<#53673#> <#53674#>0<#53674#> <#53675#>0<#53675#> <#53676#>0))<#53676#>
<#53677#>(define<#53677#> <#53678#>Y<#53678#> <#53679#>X)<#53679#> 
<#53680#>(b<#53680#><#53681#>egin<#53681#> 
  <#53682#>(vector-set!<#53682#> <#53683#>X<#53683#> <#53684#>0<#53684#> <#53685#>2)<#53685#> 
  <#53686#>(vector-set!<#53686#> <#53687#>Y<#53687#> <#53688#>1<#53688#> <#53689#>(+<#53689#> <#53690#>(vector-ref<#53690#> <#53691#>Y<#53691#> <#53692#>0)<#53692#> <#53693#>(vector-ref<#53693#> <#53694#>Y<#53694#> <#53695#>1)))<#53695#> 
  <#53696#>(vector-ref<#53696#> <#53697#>Y<#53697#> <#53698#>1))<#53698#> 
Show all steps.~ external Solution<#69392#><#69392#> <#53707#>Exercise 40.4.2<#53707#> Develop the function <#69393#><#53709#>clear<#53709#><#69393#>, which consumes a vector with three slots and sets them to <#69394#><#53710#>0<#53710#><#69394#>.~ external Solution<#69395#><#69395#> <#53716#>Exercise 40.4.3<#53716#> Develop the function <#69396#><#53718#>swap<#53718#><#69396#>, which consumes a vector with two slots and swaps the values in these slots.~ external Solution<#69397#><#69397#> <#53724#>Exercise 40.4.4<#53724#> Extend the board representation of exercise~#exvectorchess#53726> with the function
<#71967#>;; <#69398#><#53731#>board-flip!<#53731#> <#53732#>:<#53732#> <#53733#>board<#53733#> <#53734#>N<#53734#> <#53735#>N<#53735#> <#53736#><#53736#><#53737#>-;SPMgt;<#53737#><#53738#><#53738#> <#53739#>boolean<#53739#><#69398#><#71967#>
<#71968#>;; to negate the board position with indices <#69399#><#53740#>i<#53740#><#69399#>, <#69400#><#53741#>j<#53741#><#69400#> on <#69401#><#53742#>a-board<#53742#><#69401#><#71968#> 
<#53743#>(define<#53743#> <#53744#>(board-set!<#53744#> <#53745#>a-board<#53745#> <#53746#>i<#53746#> <#53747#>j)<#53747#> <#53748#>...)<#53748#> 
Don't forget to develop examples and tests for the function.~ external Solution<#69402#><#69402#>