Exercises on Processing Two Complex Inputs

external ~<#64293#>A student's solutions should state which situation was picked and should show a recipe-based solution <#21080#>and<#21080#> a simplified solution, if possible. Ask students to explain simplifications. <#64293#> <#21081#>Exercise 17.6.1<#21081#> Develop the function <#64294#><#21083#>merge<#21083#><#64294#>. It consumes two lists of numbers, sorted in ascending order. It produces a single sorted list of numbers that contains all the numbers on both inputs lists (and nothing else). A number occurs in the output as many times as it occurs on the two input lists together. Here are two examples:
  <#21088#>(merge<#21088#> <#21089#>(list<#21089#> <#21090#>1<#21090#> <#21091#>3<#21091#> <#21092#>5<#21092#> <#21093#>7<#21093#> <#21094#>9)<#21094#> <#21095#>(list<#21095#> <#21096#>0<#21096#> <#21097#>2<#21097#> <#21098#>4<#21098#> <#21099#>6<#21099#> <#21100#>8))<#21100#>
<#21101#>=<#21101#> <#21102#>(list<#21102#> <#21103#>0<#21103#> <#21104#>1<#21104#> <#21105#>2<#21105#> <#21106#>3<#21106#> <#21107#>4<#21107#> <#21108#>5<#21108#> <#21109#>6<#21109#> <#21110#>7<#21110#> <#21111#>8<#21111#> <#21112#>9)<#21112#> 
  <#21120#>(merge<#21120#> <#21121#>(list<#21121#> <#21122#>1<#21122#> <#21123#>8<#21123#> <#21124#>8<#21124#> <#21125#>11<#21125#> <#21126#>12)<#21126#> <#21127#>(list<#21127#> <#21128#>2<#21128#> <#21129#>3<#21129#> <#21130#>4<#21130#> <#21131#>8<#21131#> <#21132#>13<#21132#> <#21133#>14))<#21133#>
<#21134#>=<#21134#> <#21135#>(list<#21135#> <#21136#>1<#21136#> <#21137#>2<#21137#> <#21138#>3<#21138#> <#21139#>4<#21139#> <#21140#>8<#21140#> <#21141#>8<#21141#> <#21142#>8<#21142#> <#21143#>11<#21143#> <#21144#>12<#21144#> <#21145#>13<#21145#> <#21146#>14)<#21146#> 
Make up some more. external Solution<#64295#><#64295#> <#21155#>Exercise 17.6.2<#21155#> The goal of this exercise is to develop a version of the Hangman game of section~#sechangman#21157> for words of arbitrary length. Provide a data definition data definition for representing words of arbitrary length with lists. A <#21158#>letter<#21158#> is represented with the symbols <#64296#><#21159#>'<#21159#><#21160#>a<#21160#><#64296#> through <#64297#><#21161#>'<#21161#><#21162#>z<#21162#><#64297#> plus <#64298#><#21163#>'<#21163#><#21164#>_<#21164#><#64298#>. Develop the function <#64299#><#21165#>reveal-list<#21165#><#64299#>. It consumes three arguments:
  1. the <#21167#>chosen<#21167#> word, which is the word that we have to guess;
  2. the <#21168#>status<#21168#> word, which states how much of the word we have guessed so far; and
  3. a letter, which is our current <#21169#>guess<#21169#>.
It produces a new status word, that is, a word that contains ordinary letters and <#64300#><#21171#>'<#21171#><#21172#>_<#21172#><#64300#>. The fields in the new status word are determined by comparing the guess with each pair of letters from the status word and the chosen word:
  1. If the guess is equal to the letter in the chosen word, the guess is the corresponding letter in the new status word.
  2. Otherwise, the new letter is the corresponding letter from the status word.
Test the function with the following examples:
  1. <#64301#><#21176#>(reveal<#21176#>\ <#21177#>(list<#21177#>\ <#21178#>'<#21178#><#21179#>t<#21179#>\ <#21180#>'<#21180#><#21181#>e<#21181#>\ <#21182#>'<#21182#><#21183#>a)<#21183#>\ <#21184#>(list<#21184#>\ <#21185#>'<#21185#><#21186#>_<#21186#>\ <#21187#>'<#21187#><#21188#>e<#21188#>\ <#21189#>'<#21189#><#21190#>_<#21190#><#21191#>)<#21191#>\ <#21192#>'<#21192#><#21193#>u)<#21193#><#64301#>
  2. <#64302#><#21194#>(reveal<#21194#>\ <#21195#>(list<#21195#>\ <#21196#>'<#21196#><#21197#>a<#21197#>\ <#21198#>'<#21198#><#21199#>l<#21199#>\ <#21200#>'<#21200#><#21201#>e)<#21201#>\ <#21202#>(list<#21202#>\ <#21203#>'<#21203#><#21204#>a<#21204#>\ <#21205#>'<#21205#><#21206#>_<#21206#>\ <#21207#>'<#21207#><#21208#>_<#21208#><#21209#>)<#21209#>\ <#21210#>'<#21210#><#21211#>e)<#21211#><#64302#>
  3. <#64303#><#21212#>(reveal<#21212#>\ <#21213#>(list<#21213#>\ <#21214#>'<#21214#><#21215#>a<#21215#>\ <#21216#>'<#21216#><#21217#>l<#21217#>\ <#21218#>'<#21218#><#21219#>l)<#21219#>\ <#21220#>(list<#21220#>\ <#21221#>'<#21221#><#21222#>_<#21222#>\ <#21223#>'<#21223#><#21224#>_<#21224#>\ <#21225#>'<#21225#><#21226#>_<#21226#><#21227#>)<#21227#>\ <#21228#>'<#21228#><#21229#>l)<#21229#><#64303#>
First determine what the result should be. Use the teachpack <#21231#>hangman.ss<#21231#> and the functions <#64304#><#21232#>draw-next-part<#21232#><#64304#> (from exercise~#exdrawnext#21233>) and <#64305#><#21234#>reveal-list<#21234#><#64305#> to play the Hangman game. Evaluate the following expression:
<#21239#>(hangman-list<#21239#> <#21240#>reveal-list<#21240#> <#21241#>draw-next-part)<#21241#>
The function <#64306#><#21245#>hangman-list<#21245#><#64306#> choses a word randomly and pops up a window with a choice menu for letters. Choose letters and, when ready, click on the Check button to see whether your guess is correct. Enjoy!~ external Solution<#64307#><#64307#> <#21251#>Exercise 17.6.3<#21251#> In a factory, employees punch time cards as they arrive in the morning and leave in the evening. In the modern age of electronic punch cards, a punch card contains an employee number and the number of hours worked. Also, employee records always contain the name of the employee, an employee number, and a pay rate. Develop the function <#71150#><#21253#>hours<#21253#><#64308#><#21254#><#21254#><#21255#>-;SPMgt;<#21255#><#21256#><#21256#><#64308#><#21257#>wages2<#21257#><#71150#>. The function consumes a list of employee records and a list of (electronic) punch cards. It computes the weekly wage for each employee by matching the employee record with a punch card based on employee numbers. If a pair is missing or if a pair's employee numbers are mismatched, the function stops with an appropriate error message. Assume that there is at most one card per employee and employee number. <#21258#>Hint:<#21258#> \ An accountant would sort the two lists by employee number first.~ external Solution<#64309#><#64309#> <#21264#>Exercise 17.6.4<#21264#> A <#21266#>linear combination<#21266#> is the sum of many linear terms, <#21267#>i.e.<#21267#>, products of variables and numbers. The latter are called coefficients in this context. Here are some examples:

#displaymath73084#

In all three examples, the coefficient of <#21271#>x<#21271#> is 5, that of <#21272#>y<#21272#> is 17, and the one for <#21273#>z<#21273#> is 3. If we are given values for variables, we can determine the value of a polynomial. For example, if x = 10, the value of #tex2html_wrap_inline73088# is 50; if x = 10 and y = 1, the value of #tex2html_wrap_inline73096# is 67; and if x = 10, y = 1, and z = 2, the value of #tex2html_wrap_inline73106# is 73. In the past, we would have developed functions to compute the values of linear combinations for specific values. An alternative representation is a list of its coefficients. The above combinations would be represented as:

<#21278#>(list<#21278#> <#21279#>5)<#21279#>
<#21280#>(list<#21280#> <#21281#>5<#21281#> <#21282#>17)<#21282#> 
<#21283#>(list<#21283#> <#21284#>5<#21284#> <#21285#>17<#21285#> <#21286#>3)<#21286#> 
This representation assumes that we always agree on using variables in a fixed order. Develop the function <#64310#><#21290#>value<#21290#><#64310#>. It consumes the representation of a polynomial and a list of numbers. The lists are of equal length. It produces the value of the polynomial for these values.~ external Solution<#64311#><#64311#> <#21296#>Exercise 17.6.5<#21296#> external ~<#64312#>This exercise requires #expermutations#21299> and #exrandom#21300>. Provide students with solutions.<#64312#> Louise, Jane, Laura, Dana, and Mary are sisters who would like to save money and work on Christmas gifts. So they decide to hold a lottery, which assigns to each one of them a single gift recipient. Since Jane is a computer programmer, they ask her to write a program that performs the lottery in an impartial manner. Of course, the program must not assign any of the sisters to herself. Here is the definition of <#64313#><#21301#>gift-pick<#21301#><#64313#>. It consumes a list of distinct names (symbols) and randomly picks one of those arrangements of the list that do not agree with the original list at any position.
<#71151#>;; <#64314#><#21306#>gift-pick:<#21306#> <#21307#>list-of-names<#21307#> <#21308#><#21308#><#21309#>-;SPMgt;<#21309#><#21310#><#21310#> <#21311#>list-of-names<#21311#><#64314#><#71151#>
<#71152#>;; to pick a ``random'' non-identity arrangement of <#64315#><#21312#>names<#21312#><#64315#><#71152#> 
<#21313#>(d<#21313#><#21314#>efine<#21314#> <#21315#>(gift-pick<#21315#> <#21316#>names)<#21316#> 
  <#21317#>(r<#21317#><#21318#>andom-pick<#21318#> 
    <#21319#>(non-same<#21319#> <#21320#>names<#21320#> <#21321#>(arrangements<#21321#> <#21322#>names))))<#21322#> 
Recall that <#64316#><#21326#>arrangements<#21326#><#64316#> (see exercise~#expermutations#21327>) consumes a list of symbols and produces the list of all re-arrangements of the items in the list. Develop the auxiliary functions
  1. <#64317#><#21329#>random-pick<#21329#>\ <#21330#>:<#21330#>\ <#21331#>list-of-list-of-names<#21331#>\ <#21332#><#21332#><#21333#>-;SPMgt;<#21333#><#21334#><#21334#>\ <#21335#>list-of-names<#21335#><#64317#>, which consumes a list of items and randomly picks one of them as the result;
  2. <#64318#><#21336#>non-same<#21336#>\ <#21337#>:<#21337#>\ <#21338#>list-of-names<#21338#>\ <#21339#>list-of-list-of-names<#21339#>\ <#21340#><#21340#><#21341#>-;SPMgt;<#21341#><#21342#><#21342#>\ <#21343#>list-of-list-of-names<#21343#><#64318#>, which consumes a list of names <#64319#><#21344#>L<#21344#><#64319#> and a list of arrangements and produces the list of those that do not agree with <#64320#><#21345#>L<#21345#><#64320#> at any position. Two permutations agree at some position if we can extract the same name from both lists by applying <#64321#><#21346#>first<#21346#><#64321#> and the same number of <#64322#><#21347#>rest<#21347#><#64322#> operations to both. For example, <#64323#><#21348#>(list<#21348#>\ <#21349#>'<#21349#><#21350#>a<#21350#>\ <#21351#>'<#21351#><#21352#>b<#21352#>\ <#21353#>'<#21353#><#21354#>c)<#21354#><#64323#> and <#64324#><#21355#>(list<#21355#>\ <#21356#>'<#21356#><#21357#>c<#21357#>\ <#21358#>'<#21358#><#21359#>a<#21359#>\ <#21360#>'<#21360#><#21361#>b)<#21361#><#64324#> do not agree, but <#64325#><#21362#>(list<#21362#>\ <#21363#>'<#21363#><#21364#>a<#21364#>\ <#21365#>'<#21365#><#21366#>b<#21366#>\ <#21367#>'<#21367#><#21368#>c)<#21368#><#64325#> and <#64326#><#21369#>(list<#21369#>\ <#21370#>'<#21370#><#21371#>c<#21371#>\ <#21372#>'<#21372#><#21373#>b<#21373#>\ <#21374#>'<#21374#><#21375#>a)<#21375#><#64326#> agree at the second position. We can prove that by applying <#64327#><#21376#>rest<#21376#><#64327#> followed by <#64328#><#21377#>first<#21377#><#64328#> to both lists.
Follow the appropriate recipe in each case carefully. <#21379#>Hint:<#21379#> \ Recall that <#64329#><#21380#>(random<#21380#>\ <#21381#>n)<#21381#><#64329#> picks a random number between <#64330#><#21382#>0<#21382#><#64330#> and <#64331#><#21383#>n<#21383#><#64331#> (cmp. exercise~#exrandom#21384>).~ external Solution<#64332#><#64332#> <#21390#>Exercise 17.6.6<#21390#> Develop the function <#64333#><#21392#>DNAprefix<#21392#><#64333#>. The function takes two arguments, both lists of symbols (only <#64334#><#21393#>'<#21393#><#21394#>a<#21394#><#64334#>, <#64335#><#21395#>'<#21395#><#21396#>c<#21396#><#64335#>, <#64336#><#21397#>'<#21397#><#21398#>g<#21398#><#64336#>, and <#64337#><#21399#>'<#21399#><#21400#>t<#21400#><#64337#> occur in DNA, but we can safely ignore this issue here). The first list is called a <#21401#>pattern<#21401#>, the second one a <#21402#>search-string<#21402#>. The function returns <#64338#><#21403#>true<#21403#><#64338#> if the pattern is a prefix of the search-string. In all other cases, the function returns <#64339#><#21404#>false<#21404#><#64339#>, <#21405#>e.g.<#21405#>,
  <#21410#>(DNAprefix<#21410#> <#21411#>(list<#21411#> <#21412#>'<#21412#><#21413#>a<#21413#> <#21414#>'<#21414#><#21415#>t)<#21415#> <#21416#>(list<#21416#> <#21417#>'<#21417#><#21418#>a<#21418#> <#21419#>'<#21419#><#21420#>t<#21420#> <#21421#>'<#21421#><#21422#>c))<#21422#> 
<#21423#>=<#21423#> <#21424#>true<#21424#> 
  <#21432#>(DNAprefix<#21432#> <#21433#>(list<#21433#> <#21434#>'<#21434#><#21435#>a<#21435#> <#21436#>'<#21436#><#21437#>t)<#21437#> <#21438#>(list<#21438#> <#21439#>'<#21439#><#21440#>a))<#21440#> 
<#21441#>=<#21441#> <#21442#>false<#21442#> 
  <#21450#>(DNAprefix<#21450#> <#21451#>(list<#21451#> <#21452#>'<#21452#><#21453#>a<#21453#> <#21454#>'<#21454#><#21455#>t)<#21455#> <#21456#>(list<#21456#> <#21457#>'<#21457#><#21458#>a<#21458#> <#21459#>'<#21459#><#21460#>t))<#21460#> 
<#21461#>=<#21461#> <#21462#>true<#21462#> 
  <#21470#>(DNAprefix<#21470#> <#21471#>(list<#21471#> <#21472#>'<#21472#><#21473#>a<#21473#> <#21474#>'<#21474#><#21475#>c<#21475#> <#21476#>'<#21476#><#21477#>g<#21477#> <#21478#>'<#21478#><#21479#>t)<#21479#> <#21480#>(list<#21480#> <#21481#>'<#21481#><#21482#>a<#21482#> <#21483#>'<#21483#><#21484#>g))<#21484#>
<#21485#>=<#21485#> <#21486#>false<#21486#> 
  <#21494#>(DNAprefix<#21494#> <#21495#>(list<#21495#> <#21496#>'<#21496#><#21497#>a<#21497#> <#21498#>'<#21498#><#21499#>a<#21499#> <#21500#>'<#21500#><#21501#>c<#21501#> <#21502#>'<#21502#><#21503#>c)<#21503#> <#21504#>(list<#21504#> <#21505#>'<#21505#><#21506#>a<#21506#> <#21507#>'<#21507#><#21508#>c))<#21508#>
<#21509#>=<#21509#> <#21510#>false<#21510#> 
Simplify <#64340#><#21514#>DNAprefix<#21514#><#64340#>, if possible. Modify <#64341#><#21515#>DNAprefix<#21515#><#64341#> so that it returns the first item beyond the pattern in the search-string if it the pattern is a proper prefix of the search-string. If the lists do not match or if the pattern is no shorter than the search-string, the modified function should still return <#64342#><#21516#>false<#21516#><#64342#>. Similarly, if the lists are equally long and match, the result is still <#64343#><#21517#>true<#21517#><#64343#>:
  <#21522#>(DNAprefix<#21522#> <#21523#>(list<#21523#> <#21524#>'<#21524#><#21525#>a<#21525#> <#21526#>'<#21526#><#21527#>t)<#21527#> <#21528#>(list<#21528#> <#21529#>'<#21529#><#21530#>a<#21530#> <#21531#>'<#21531#><#21532#>t<#21532#> <#21533#>'<#21533#><#21534#>c))<#21534#> 
<#21535#>=<#21535#> <#21536#>'<#21536#><#21537#>c<#21537#> 
  <#21545#>(DNAprefix<#21545#> <#21546#>(list<#21546#> <#21547#>'<#21547#><#21548#>a<#21548#> <#21549#>'<#21549#><#21550#>t)<#21550#> <#21551#>(list<#21551#> <#21552#>'<#21552#><#21553#>a))<#21553#> 
<#21554#>=<#21554#> <#21555#>false<#21555#> 
  <#21563#>(DNAprefix<#21563#> <#21564#>(list<#21564#> <#21565#>'<#21565#><#21566#>a<#21566#> <#21567#>'<#21567#><#21568#>t)<#21568#> <#21569#>(list<#21569#> <#21570#>'<#21570#><#21571#>a<#21571#> <#21572#>'<#21572#><#21573#>t))<#21573#> 
<#21574#>=<#21574#> <#21575#>true<#21575#> 
Can this variant of <#64344#><#21579#>DNAprefix<#21579#><#64344#> be simplified? If so, do it. If not, explain why. external Solution<#64345#><#64345#>